Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中通过蓝牙发送事件_Android_Events_Bluetooth - Fatal编程技术网

android中通过蓝牙发送事件

android中通过蓝牙发送事件,android,events,bluetooth,Android,Events,Bluetooth,我已在两个电话之间创建蓝牙连接配对的设备没有我的应用程序。如何向手机发送屏幕锁定/音量增大等事件 BluetoothAdapter BluetoothAdapter=BluetoothAdapter.getDefaultAdapter; 如果bluetoothAdapter.i已启用{ handler.sendEmptyMessageDelayedconnectionCheckTimeout,30000; BluetoothReceiver BluetoothReceiver=新的Bluetoo

我已在两个电话之间创建蓝牙连接配对的设备没有我的应用程序。如何向手机发送屏幕锁定/音量增大等事件

BluetoothAdapter BluetoothAdapter=BluetoothAdapter.getDefaultAdapter; 如果bluetoothAdapter.i已启用{ handler.sendEmptyMessageDelayedconnectionCheckTimeout,30000; BluetoothReceiver BluetoothReceiver=新的BluetoothReceiver; 蓝牙接收器。注册蓝牙接收器用于连接此; SocketThread SocketThread=SocketThread.getInstance; registerBluetoothReceiveforCommunications这是,this; Thread Thread=新的ThreadsocketThread; thread.start; }

我的socket线程类在这里,我得到的是一个连接的设备,而不是刚刚配对的。 现在,我必须将音量增大/减小等事件发送到另一个不运行我的应用程序的设备

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

}蓝牙发送原始数据。显然,你必须以某种方式解析这些内容,并将其转换为应用程序中的调用,做一些工作

到目前为止你做了什么?请共享您的代码。请在此处查找更新的代码。我必须发送原始数据,如音量上升/下降事件。怎么做?