Unity实现Android源生平台蓝牙BLE4.0数据传输(三)

阅读: 评论:0

Unity实现Android源生平台蓝牙BLE4.0数据传输(三)

Unity实现Android源生平台蓝牙BLE4.0数据传输(三)

上篇我说到如何打开系统蓝牙 扫描设备 连接 具体连接我们在这里说

 AndroidJavaObject wfzBluetoothGattCallback = new AndroidJavaObject("com.wfz.bletounity.WFZBluetoothGattCallback”);//连接的事件回调对象BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback();//unity事件回调接口的实现类//特征值数据变化的事件委托CharacteristicChangedDelegate = (g, c) =>{var data = c.Call<byte[]>("getValue");string d = "";for (int i = 0; i < data.Length; i++){d += data[i];}Debug.Log(d);};//设备连接状态变化的事件委托ConnectionStateChangeDelegate = (g, s, n) =>{switch (n){case 0:Debug.Log("断开连接");break;case 1:Debug.Log("连接中");break;case 2:Debug.Log("连接成功");Debug.Log("获取到gatt对象"+g.Call<AndroidJavaObject>("getDevice").Call<string>("getName"));var isFind= g.Call<bool>("discoverServices");break;case 3:Debug.Log("断开中");break;default:break;}};
//发现设备服务事件委托ServicesDiscoveredDelegate = (g, c) =>{var  gattServices = g.Call<AndroidJavaObject>("getServices");Debug.Log("发现新服务..." + gattServices.Get<int>("size"));if (gattCharacteristic==null){gattCharacteristic = GetCharacteristic(GetService(g, serviceID), characteristicID);NotificationData(g, gattCharacteristic, descriptorID);}};

回调对象写好了我们就可以连接设备了

wfzBluetoothGattCallback.Call("AddUnityCallback", bluetoothGattCallback);//把实现的接口添加到回调对象buetoothGatt = device.Call<AndroidJavaObject>("connectGatt", AndroidCall.unityActivity, true, wfzBluetoothGattCallback);//开始连接
下面是获取服务 特征值的函数实现

  /// <summary>/// Gets the service./// </summary>/// <returns>The service.</returns>/// <param name="gatt">Gatt.</param>/// <param name="uuid">UUID.</param>public AndroidJavaObject GetService(AndroidJavaObject gatt, string uuid){return gatt.Call<AndroidJavaObject>("getService", ToAndroidUUID(uuid));}/// <summary>/// Gets the characteristic./// </summary>/// <returns>The characteristic.</returns>/// <param name="Service">Service.</param>/// <param name="uuid">UUID.</param>public  AndroidJavaObject GetCharacteristic(AndroidJavaObject Service,string uuid){return Service.Call<AndroidJavaObject>("getCharacteristic", ToAndroidUUID(uuid));}


打开特征值的通知

  /// <summary>/// Notifications the data./// </summary>/// <param name="gatt">Gatt.</param>/// <param name="characteristic">Characteristic.</param>/// <param name="uuid">UUID.</param>public void NotificationData(AndroidJavaObject gatt,AndroidJavaObject characteristic,string uuid){var b = gatt.Call<bool>("setCharacteristicNotification", characteristic, true);if (b){var bluetoothGattDescriptor = new AndroidJavaClass("android.bluetooth.BluetoothGattDescriptor");var descriptor= characteristic.Call<AndroidJavaObject>("getDescriptor",ToAndroidUUID(uuid));var  isSet=  descriptor.Call<bool>("setValue", bluetoothGattDescriptor.GetStatic<byte[]>("ENABLE_NOTIFICATION_VALUE"));if (isSet){var iSwrite=  gatt.Call<bool>("writeDescriptor", descriptor);if (iSwrite){Debug.Log("打开特性通知");}}}}
打开通知之后 就可以在onCharacteristicChanged 事件里面获取到发送过来的数据了

有问题的朋友可以关注我的微信订阅号给我留言

Unity实现Android源生平台蓝牙BLE4.0数据传输(一)

Unity实现Android源生平台蓝牙BLE4.0数据传输(二)



本文发布于:2024-02-02 04:26:21,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170681918041347.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:蓝牙   数据传输   生平   Unity   Android
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23