java 在 Android Studio 中配对蓝牙设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46841534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 09:22:38  来源:igfitidea点击:

Pair a bluetooth device in Android Studio

javaandroidbluetooth

提问by Kirchhoff1415

I'm creating an app that should connect via bluetooth to a specific device.

我正在创建一个应通过蓝牙连接到特定设备的应用程序。

I want my app to connect with this device no matter it is already paired or not.

我希望我的应用程序与此设备连接,无论它是否已配对。

For now I have this

现在我有这个

private void findDevice() {
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if (device.getName().equals(DEVICE_NAME)) {
                bluetoothDevice = device;
                deviceFound = true;
                break;
            }
        }
    }
}

But this function connects only to paired devices. If my device isn't already paired, I want to pair it. Have no idea how to do this.

但此功能仅连接到配对设备。如果我的设备尚未配对,我想配对它。不知道该怎么做。

Can someone get me any advice please?

有人可以给我任何建议吗?

回答by nhoxbypass

First request BLUETOOTH_ADMINpermission.

首先请求BLUETOOTH_ADMIN许可。

Then make your device discoverable:

然后使您的设备可被发现:

private void makeDiscoverable() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(discoverableIntent);
        Log.i("Log", "Discoverable ");
    }

Then create a BroadcastReceiverto listen to action from system:

然后创建一个BroadcastReceiver来监听来自系统的动作:

private BroadcastReceiver myReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Message msg = Message.obtain();
            String action = intent.getAction();
            if(BluetoothDevice.ACTION_FOUND.equals(action)){
               //Found, add to a device list
            }           
        }
    };

And start searching for devices by registering this BoardcastReceiver:

并通过注册此BoardcastReceiver开始搜索设备:

 private void startSearching() {
        Log.i("Log", "in the start searching method");
        IntentFilter intentFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        BluetoothDemo.this.registerReceiver(myReceiver, intentFilter);
        bluetoothAdapter.startDiscovery();
    }

After devices come from the BroadcastReceiverinto a list, select your device from the list and createBond()with this:

设备从BroadcastReceiver进入列表后,从列表中选择您的设备,然后createBond()

 public boolean createBond(BluetoothDevice btDevice)  
    throws Exception  
    { 
        Class class1 = Class.forName("android.bluetooth.BluetoothDevice");
        Method createBondMethod = class1.getMethod("createBond");  
        Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);  
        return returnValue.booleanValue();  
    } 

Then use your code above to manage with bonded devices.

然后使用上面的代码来管理绑定设备。