我正在尝试将一个字节数组从6.0.1安卓设备发送到RN4020蓝牙模块。到目前为止,我已经能够发现,结合并连接到模块。我正在尝试使用内置的16位UUID发送字节数组,以便在没有响应的情况下进行写入。当我使用我的代码时,我没有收到任何错误,字节数组也没有被发送。我做错了什么?
这是我的代码:
namespace Remote
{
[Activity(Label = "Remote", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;
const string UniversalUUID = "00001101-0000-1000-8000-00805F9B34FB";
const string MAC = "00:1E:C0:22:3D:0E";
ICollection<BluetoothDevice> pairedDevices;
BluetoothAdapter adapter;
BluetoothDevice device;
BluetoothGatt gatt;
BluetoothGattCallback callBack;
BluetoothManager manager;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button test = FindViewById<Button>(Resource.Id.Test);
EditText MACs = FindViewById<EditText>(Resource.Id.MACs);
pairedDevices = getAllPairedDevices();
test.Click += delegate
{
connected();
byte[] buffer = new byte [] { 124, 079, 044, 048, 052, 044, 048, 052, 013, 010};//stringToByteArray("|O,04,04\r");
BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(UUID.NameUUIDFromBytes(new byte[] { 0, 4}), GattProperty.Write, GattPermission.Write);
characteristic.SetValue(buffer);
gatt.WriteCharacteristic(characteristic);
};
private ICollection<BluetoothDevice> getAllPairedDevices()
{
BluetoothAdapter btAdapter = BluetoothAdapter.DefaultAdapter;
return btAdapter.BondedDevices;
}
private async Task<bool> connected()
{
manager = (BluetoothManager)GetSystemService(Context.BluetoothService);
adapter = manager.Adapter;
device = adapter.GetRemoteDevice(MAC);
gatt = device.ConnectGatt(this, false, callBack);
return true;
}
}
}
答案 0 :(得分:0)
我不知道为什么BluetoothGattCharacteristic的构造函数是公开的。获取BluetoothGattCharacteristic的正确方法是通过调用gatt.DiscoverServices()来进行服务发现。当您获得服务发现完成的回调时,您可以通过gatt.GetServices()获取所有服务和特征。
在您的代码中,您也不会等到连接完毕。 device.ConnectGatt方法是异步的,意味着它启动连接尝试。当您的设备连接/断开连接时,将调用您的回调对象的OnConnectionStateChange。