Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.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_Bluetooth - Fatal编程技术网

Android 如何知道蓝牙配对何时完成

Android 如何知道蓝牙配对何时完成,android,bluetooth,Android,Bluetooth,配对完成后,我想通过蓝牙监听传入的消息。在配对完成之前,我无法呼叫listen(可以吗?),也不知道何时完成 我尝试了很多方法在两台设备之间配对,比如: private void pairDevice() { try { BluetoothDevice device = bluetooth.getRemoteDevice(address); Log.d("pairDevice()", "Start Pairing..."); Meth

配对完成后,我想通过蓝牙监听传入的消息。在配对完成之前,我无法呼叫listen(可以吗?),也不知道何时完成

我尝试了很多方法在两台设备之间配对,比如:

   private void pairDevice() {
    try {
        BluetoothDevice device = bluetooth.getRemoteDevice(address);
        Log.d("pairDevice()", "Start Pairing...");
        Method m = device.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
        Log.d("pairDevice()", "Pairing finished.");
    } catch (Exception e) {
        Log.e("pairDevice()", e.getMessage());
    }
}
这个很好用,但它会打开一个弹出窗口来接受配对和
Log.d(“pairDevice(),“配对完成”)
在弹出对话框被接受之前被调用,我只需要在配对完全完成且设备现在已配对时执行操作。以下是我用于侦听消息的代码:

public void run() {
    final int BUFFER_SIZE = 1024;
    byte[] buffer = new byte[BUFFER_SIZE];
    int bytes = 0;
    int b = BUFFER_SIZE;
    while (true) {
        try {
            bytes = inStream.read(buffer, bytes, BUFFER_SIZE - bytes);
            textview.setText(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
这是我发送消息的代码(分数):

private void init()引发IOException{
如果(蓝牙!=null){
如果(bluetooth.isEnabled()){
Set-bondedDevices=bluetooth.getBondedDevices();
如果(bondedDevices.size()>0){
parceluid[]uuids=device.getUuids();
BluetoothSocket=device.createrFComSocketToServiceRecord(uuids[0].getUuid());
socket.connect();
outputStream=socket.getOutputStream();
inStream=socket.getInputStream();
写(分数);
}
Log.e(“错误”,“没有合适的配对设备”);
}否则{
Log.e(“错误”,“蓝牙被禁用”);
}
}
}
public void write(字符串s)引发IOException{
write(s.getBytes());
}

我尝试在没有用户许可的情况下配对两台设备,如下面的回答中所示——但它也要求用户许可,我不知道为什么。。所以实际上我不认为我需要未经允许的配对,我只需要在接受时做些事情。可能吗?谢谢。

也许这个链接可以帮助你>>>[如何通过编程判断蓝牙设备是否已连接?(Android 2.2)][1][1]:@ASHKARAN我稍后会在收到我的电脑后再试。Thx@ASHKARAN也只有当弹出窗口出现时才会发生。
  private void init() throws IOException {
    if (bluetooth != null) {
        if (bluetooth.isEnabled()) {
            Set<BluetoothDevice> bondedDevices = bluetooth.getBondedDevices();
            if (bondedDevices.size() > 0) {
                ParcelUuid[] uuids = device.getUuids();
                BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid());
                socket.connect();
                outputStream = socket.getOutputStream();
                inStream = socket.getInputStream();
                write(score);
            }
            Log.e("error", "No appropriate paired devices.");
        } else {
            Log.e("error", "Bluetooth is disabled.");
        }
    }
}

public void write(String s) throws IOException {
    outputStream.write(s.getBytes());
}