【问题标题】:Sending events through bluetooth in android在android中通过蓝牙发送事件
【发布时间】:2016-11-23 11:50:31
【问题描述】:

我在两部手机之间建立了蓝牙连接(配对的设备没有我的应用程序)。如何向手机发送屏幕锁定/音量调高等事件

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

}

【问题讨论】:

  • 到目前为止你做了什么?请分享您的代码
  • Sachith,请找到更新后的代码

标签: android events bluetooth


【解决方案1】:

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

【讨论】:

  • 在这里,我必须发送原始数据,例如音量增大/减小事件。该怎么做?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-24
  • 2016-05-07
相关资源
最近更新 更多