简体   繁体   English

通过BluetoothAdapter将蓝牙与getRemoteDevice()连接

[英]Connect bluetooth with getRemoteDevice() from BluetoothAdapter

I would like to manually connect a bluetooth device with its MAC address because it is faster and I know exactly which MAC to connect. 我想手动使用其MAC地址连接蓝牙设备,因为它速度更快,而且我确切知道要连接哪个MAC。

I use this method to get the BluetoothDevice : http://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29 我使用此方法获取BluetoothDevice: http : //developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getRemoteDevice%28byte[]%29

But the Android doc does not say if Android ensure that the device is in range before creating the BluetoothDevice object. 但是Android文档没有说明Android在创建BluetoothDevice对象之前是否确保设备在范围内。 Do you have this information ? 你有这个信息吗?

My code can automatically connect the device, and I would like to check if the target is in range before trying to connect, but without perform a large scan (which can be long...) 我的代码可以自动连接设备,并且我想在尝试连接之前检查目标是否在范围之内,但无需执行大型扫描(这可能很长...)。

When local device connects to remote device using BluetoothSocket , an exception is required. 当本地设备使用BluetoothSocket连接到远程设备时,需要一个例外。

If remote device isn't in range, It's not found 如果远程设备不在范围内,则找不到它

private class ConnectThread extends Thread {

    public ConnectThread(BluetoothDevice device, boolean isSecure, UUID sharedUUID) throws IncorrectSetupException {
        try {
            //Secure connections requires to get paired before connect
            //Insecure connections allows to connect without pairing
            if (isSecure) {
                mSocket = device.createRfcommSocketToServiceRecord(sharedUUID);
            } else {
                mSocket = device.createInsecureRfcommSocketToServiceRecord(sharedUUID);
            }
        } catch (IOException e) {
            //Is there some problem with the setup?
        }
    }

    public void run() {
        try {
            mSocket.connect();
        } catch (IOException e) {
            //If device is not found, this exception is throwed
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android BluetoothAdapter getRemoteDevice已缓存 - Android BluetoothAdapter getRemoteDevice is cached BLE:通过BluetoothAdapter#getRemoteDevice()获取BluetoothDevice与扫描 - BLE: obtaining BluetoothDevice via BluetoothAdapter#getRemoteDevice() vs scanning E / BluetoothAdapter:蓝牙活页夹为空 - E/BluetoothAdapter: Bluetooth binder is null 收到BluetoothAdapter时,从蓝牙服务器始终正常断开连接。STATE_TURNING_OFF - Consistent graceful disconnect from Bluetooth server when receiving BluetoothAdapter.STATE_TURNING_OFF 缺少BluetoothAdapter.isEnabled.BLUETOOTH所需的权限 - missing permissions required by BluetoothAdapter.isEnabled.BLUETOOTH E / BluetoothAdapter(883):蓝牙装订器为空 - E/BluetoothAdapter(883): Bluetooth binder is null 取消绑定蓝牙设备时的BluetoothAdapter通知 - BluetoothAdapter notification when a bluetooth device is unboded 使用 Xamarin.android 中的 BluetoothAdapter 执行蓝牙扫描 - Perform a bluetooth scan with BluetoothAdapter in Xamarin.android Android-ClassCastException-BluetoothManager无法强制转换为android.bluetooth.BluetoothAdapter错误 - Android - ClassCastException - BluetoothManager cannot be cast to android.bluetooth.BluetoothAdapter Error 为什么BluetoothAdapter.startDiscovery(..)需要广播蓝牙设备? - Why BluetoothAdapter.startDiscovery(..) require to get Bluetooth device broadcast?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM