2016-11-23 10 views
-3

2台の電話機の間にBluetooth接続を作成しました(ペア化したデバイスには自分のアプリがありません)。私のソケットスレッドクラスiが接続されたデバイスを取得しています、ここでアップアンドロイドでブルートゥース経由でイベントを送信する

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(); }

画面ロック/ボリュームのように携帯電話にイベントを送信する方法(ちょうどペアではありません)。 今私はアプリケーションを実行していない他のデバイスにボリュームアップ/ダウンのようなイベントを送信する必要があります。

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

これまでに何をしていますか?あなたのコードを共有してください – Sachith

+0

Sachith、更新コード –

答えて

0

Bluetoothは、生データを送信します。どうやら、あなたはそれらを何とか解析して、あなたのアプリでコールに変換して、何らかの仕事をしなければならないのですか?

+0

ここでは、ボリュームアップ/ダウンイベントのような生データを送信する必要があります。どうやってするか? –

関連する問題