如何解决使用远程蓝牙设备开始绑定过程
我已经成功制作了一个 android 设备并扫描并登记了该地区的所有蓝牙设备。我有我刚刚以字符串形式发现的远程设备的 mac 地址,我想用它开始绑定过程。 . 我尝试 BluetoothDevice
对象,然后方法 createBond()
但它没有与远程设备通信以配对..
这是代码
class BluetoothM: AppCompatActivity{
// mac address of remote bluetooth device
string address;
//the discovered devices are listed in a ListView so i call a listview item click method to start pairing
private void Dlist_ItemClick(object sender,AdapterView.ItemClickEventArgs e)
{
//the mac address has already been assigned in the OnReceive broadcast Receiver
if (e.Position == e.Id)
{
//Get the default adapter of the device
BluetoothAdapter adapt = BluetoothAdapter.DefaultAdapter;
//Get the remote device based on its MAC address
BluetoothDevice device = adapt.GetRemoteDevice(address);
//start the pairing process for the device
device.CreateBond();
}
}
}
//This class discovers bluetooth devices and pass MAC address to our string address for use in the listview click method
[broadcastReceiver(Name = BluetoothDevice.ActionFound,Enabled = true)]
public class Devicediscovered : broadcastReceiver
{
public override void OnReceive(Context context,Intent intent)
{
string ac = intent.Action; string name;
if (BluetoothDevice.ActionFound.Equals(ac))
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExTradevice);
//add bluetooth name and address if they do not already exist
if (!MainActivity.avail.Contains(device.Name + "\n" + device.Address))
{
MainActivity.avail.Add(device.Name + "\n" + device.Address);
ArrayAdapter arrayAdapter1 = new ArrayAdapter(context,Android.Resource.Layout.SimpleListItem1,MainActivity.avail);
MainActivity.dlist.Adapter = arrayAdapter1;
//address assigned a value
MainActivity.address = device.Address; MainActivity.name = device.Name;
MainActivity.mydevice = device;
}
}
Toast.MakeText(context,"Received the intent",ToastLength.Short).Show();
}
远程设备未收到此通信,我不知道为什么,谢谢
解决方法
检查您在 Manifest 中是否拥有 BLUETOOTH_ADMIN
权限。注册 ACTION_BOND_STATE_CHANGED 意图,以便在绑定过程完成及其结果时收到通知。获取 device.CreateBond() 的结果,它在立即出错时返回 false,如果绑定将开始则返回 true。
https://developer.android.com/reference/android/bluetooth/BluetoothDevice#createBond()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。