蓝牙配对谷歌玻璃 [英] Bluetooth Pairing Google Glass
问题描述
使用谷歌的玻璃,我能够发现蓝牙设备,看到他们的地址和信息。但是,我不能让玻璃以对(债券)与他们。
Using Google Glass, I am able to discover Bluetooth devices and see their address and information. However, I cannot get the Glass to pair (bond) with them.
继<一的说明href="http://www.londatiga.net/it/programming/android/how-to-programmatically-pair-or-unpair-android-bluetooth-device/"相对=nofollow>这个页面现在我试图让粘合,但由于某些原因, BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(动作)
从不发生的事情。
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.
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();
}
}
在LOG我总是得到配对设备名称,但是当我搜索保税设备,但它仍然是空的。
In the LOG I always get "Pairing DeviceName" but when I search for the bonded devices, it remains empty.
任何帮助将大大AP preciated。
Any help will be greatly appreciated.
推荐答案
所以,我会回答我的问题,因为我刚刚发现的方法。
So I will answer my own question as I just found the way.
因此,首先,设备的发现是很容易的,在的onCreate()
我用(除所有其他种类的$ C $的c您所需要的):
So first, the discovery of devices is quite easy, in onCreate()
I used (besides all other sort of code you need):
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); // ******
然后在的BroadcastReceiver
您需要管理的设备和配对请求:
Then at the BroadcastReceiver
you need to manage the devices and the pairing requests:
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();
}
}
}
};
在该设备结合,您可以管理与UUID的连接和插座一样在Android的网页例如。
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.
希望这有助于!
这篇关于蓝牙配对谷歌玻璃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!