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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15507559/article/details/79149801

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

 AndroidJavaObject wfzBluetoothGattCallback = new AndroidJavaObject("com.wfz.bletounity.WFZBluetoothGattCallback”);//连接的事件回调对象
        BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback();//unity事件回调接口的实现类

//特征值数据变化的事件委托
        bluetoothGattCallback.onCharacteristicChangedDelegate = (g, c) =>
        {
            var data = c.Call<byte[]>("getValue");
            string d = "";
            for (int i = 0; i < data.Length; i++)
            {
                d += data[i];
            }
            Debug.Log(d);
        };

//设备连接状态变化的事件委托
        bluetoothGattCallback.onConnectionStateChangeDelegate = (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;
            }
        };
//发现设备服务事件委托
        bluetoothGattCallback.onServicesDiscoveredDelegate = (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数据传输(二)



猜你喜欢

转载自blog.csdn.net/qq_15507559/article/details/79149801