/// <summary> /// Connects the gatt. /// </summary> /// <param name="device">Device.</param> void ConnectGatt(AndroidJavaObject device) { AndroidJavaObject wfzBluetoothGattCallback = new AndroidJavaObject("com.ridk.unityextension.RidkBluetoothGattCallback"); BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback(); 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> /// /// </summary> public virtual void Open() { if (m_Device != null) { Close(); } #if UNITY_EDITOR || UNITY_STANDALONE BluetoothAdapter adapter = BluetoothAdapter.GetDefaultAdapter(); #elif UNITY_ANDROID using (BluetoothAdapter adapter = BluetoothAdapter.GetDefaultAdapter()) #endif { if (!adapter.IsEnabled()) { return; } m_Device = adapter.GetRemoteDevice(m_Address); // <!-- if (m_Device == null) { if (m_OpenCallback != null) { m_OpenCallback.OnStreamOpenFailure(this); } else { Log.e("BleSerialPort", "No m_OpenCallback"); //BluetoothScannerGUI.RequestAddress(Open); } return; } // --> m_Gatt = m_Device.ConnectGatt( #if UNITY_EDITOR || UNITY_STANDALONE Csr.Bluetooth.Object.NULL_PTR, #elif UNITY_ANDROID Android.App.Activity.currentActivity.m_SealedPtr, #endif false, m_Callback = new BluetoothGattCallback(this) ); } m_BufferRead = new byte[m_SizeRead = 0]; }
/// <summary> /// /// </summary> public virtual void OpenDevice(string i_address) { string text = i_address; //m_TxtAddress.text; if (!string.IsNullOrEmpty(text)) { if (m_Adapter == null) { m_Adapter = BluetoothAdapter.GetDefaultAdapter(); m_Cb = new BluetoothGattCallback(this); } #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN// Log.i(text, NativeMethods.libcsr_ext_address_to_string_1(m_Adapter.AddressPool[text])); m_Gatt = m_Adapter.GetRemoteDevice(text).ConnectGatt(Csr.Bluetooth.Object.NULL_PTR, false, m_Cb); #elif UNITY_ANDROID m_Gatt = m_Adapter.GetRemoteDevice(text).ConnectGatt(null, false, m_Cb); #endif } }
public BleDeviceService(BleCallback gattCallback) { CallBack = gattCallback; mGattCallback = new GattCallback(this); }