简体   繁体   English

蓝牙配对Google Glass

[英]Bluetooth Pairing Google Glass

Using Google Glass, I am able to discover Bluetooth devices and see their address and information. 使用Google Glass,我可以发现蓝牙设备并查看其地址和信息。 However, I cannot get the Glass to pair (bond) with them. 但是,我无法让Glass与它们配对(绑定)。

Update 更新

Following the instructions on this page now I'm trying to get the bonding, but for some reason the BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action) is never happening. 现在,按照页面上的说明进行操作,但是由于某种原因, BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)从未发生。

private void pairDevice(BluetoothDevice Ddevice) {
    Log.d("MY_LOG", "Try to pair " + Ddevice.getName());
    try{
        Method m = Ddevice.getClass().getMethod("createBond", (Class[]) null);
        m.invoke(Ddevice, (Object[]) null);
        Log.d("MY_LOG", "Pairing " + Ddevice.getName());
    }catch(Exception e){
        Log.d("MY_LOG", "Error: ");
        e.printStackTrace();
    }
}

In the LOG I always get "Pairing DeviceName" but when I search for the bonded devices, it remains empty. 在日志中,我总是获得“配对设备名称”,但是当我搜索绑定的设备时,它仍然为空。

Any help will be greatly appreciated. 任何帮助将不胜感激。

So I will answer my own question as I just found the way. 因此,我将在找到方法的同时回答自己的问题。

So first, the discovery of devices is quite easy, in onCreate() I used (besides all other sort of code you need): 因此,首先,在我使用的onCreate()发现设备非常容易(除了您需要的所有其他代码):

MyBT = BluetoothAdapter.getDefaultAdapter();
MyBT.startDiscovery();
Filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);                    // Register the BroadcastReceiver
Filter2 = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);         // Register the Bond changing state
registerReceiver(mReceiver, Filter);                                        // Don't forget to unregister during onDestroy
registerReceiver(mReceiver, Filter2);                                       // ******

Then at the BroadcastReceiver you need to manage the devices and the pairing requests: 然后,您需要在BroadcastReceiver上管理设备和配对请求:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {           // Create a BroadcastReceiver for ACTION_FOUND
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        if (BluetoothDevice.ACTION_FOUND.equals(action)) {                      // When discovery finds a device
            BluetoothDevice BTdevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); // Get the BluetoothDevice object from the Intent
            ListDev.add(BTdevice);                                              // Add the device to an array adapter to show...
        }     
        if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
            BluetoothDevice device = ListDev.get(selectedDevice);
            byte[] pinBytes = getStrFromName(device.getName(),7,11).getBytes();  // My devices had their own pin in their name, you can put a constant pin here...  
            try {
                Log.d("MY_LOG", "Try to set the PIN");
                Method m = device.getClass().getMethod("setPin", byte[].class);
                m.invoke(device, pinBytes);
                Log.d("MY_LOG", "Success to add the PIN.");
                try {
                    device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
                    Log.d("MY_LOG", "Success to setPairingConfirmation.");
                } catch (Exception e) {
                    Log.e("MY_LOG", e.getMessage());
                    e.printStackTrace();
                } 

            } catch (Exception e) {
                Log.e("MY_LOG", e.getMessage());
                e.printStackTrace();
            }
        }
    }
}; 

After that the device is bonded and you can manage the connection with the UUID and the sockets just as in the Android webpage example . 之后,将设备绑定,就可以像Android 网页示例中一样使用UUID和套接字管理连接。

Hope this helps! 希望这可以帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM