2016-11-23 3 views
-3

Ich habe eine Bluetooth-Verbindung zwischen zwei Telefonen erstellt (das gekoppelte Gerät habe nicht meine App). Wie Ereignisse in das Telefon wie Bildschirmsperre/VolumenSenden von Ereignissen über Bluetooth in Android

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter.isEnabled()){ handler.sendEmptyMessageDelayed(connectionCheckTimeout,30000); BluetoothReciever bluetoothReciever = new BluetoothReciever(); bluetoothReciever.registerBluetoothRecieverForConnection(this,this); SocketThread socketThread = SocketThread.getInstance(); socketThread.registerBluetoothRecieverForCommunication(this,this); Thread thread = new Thread(socketThread); thread.start(); }

my socket Thread-Klasse hier bis senden, ich bin ein angeschlossenes Gerät bekommen (nicht nur gepaart). Jetzt muss ich Ereignisse wie Lautstärke nach oben/unten zu dem anderen Gerät senden, auf dem meine App nicht läuft.

public class SocketThread implements Runnable { 
private static SocketThread ourInstance = new SocketThread(); 
BluetoothAdapter bluetoothAdapter; 
BluetoothDevice device; 
BluetoothSocket bluetoothSocket; 
static BluetoothSocketListener bluetoothSocketListener; 
public static SocketThread getInstance() { 
    return ourInstance; 
} 

private SocketThread() { 
} 

@Override 
public void run() { 

    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    device = connectedDevice(bluetoothAdapter); 
    if (device!=null){ 
     // Toast.makeText(getApplicationContext(),"connected to "+device.getName(),Toast.LENGTH_SHORT).show(); 
     if (bluetoothSocketListener!=null) { 
      bluetoothSocketListener.deviceIsConnected(device,bluetoothSocket); 
     } 

     try { 
      bluetoothSocket.getOutputStream().flush(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    }else{ 
     if (bluetoothSocketListener!=null) { 
      bluetoothSocketListener.deviceIsNotConnected(); 
     } 
    } 

} 

private BluetoothDevice connectedDevice(BluetoothAdapter bluetoothAdapter){ 
    if (bluetoothAdapter == null) 
     bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    if (bluetoothAdapter.getBondedDevices().size() == 0) 
     return null; 
    //now create socket to all paired devices. Check if connected. then return true 
    Set<BluetoothDevice> btDevices = bluetoothAdapter.getBondedDevices(); 

    for (BluetoothDevice device : btDevices){ 
     Log.d("main activity"," trying to create socket for paired device "+device.getName()); 
     try { 
      bluetoothSocket 
        = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid()); 
      bluetoothSocket.connect(); 
      if (bluetoothSocket.isConnected()){ 
       return device; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return null; 
} 

public void registerBluetoothRecieverForCommunication(Context context, BluetoothSocketListener bluetoothSocketListener){ 
    this.bluetoothSocketListener = bluetoothSocketListener; 
} 

}

+2

was Sie bis jetzt getan haben? Bitte teilen Sie Ihre Codes – Sachith

+0

Sachith, finden Sie den aktualisierten Code –

Antwort

0

Bluetooth sendet Rohdaten. Anscheinend müssen Sie diese irgendwie analysieren und in Anrufe in Ihrer APP umwandeln, die etwas Arbeit tun

+0

Hier muss ich Rohdaten wie Lautstärke hoch/runter Ereignisse senden. Wie geht das? –

Verwandte Themen