private void TryToConnect() { msg.text = "App started "; msg.text = "Search for BLE connection: " + connectBLE + " "; if (connectBLE == true) { try { msg.text += "Trying... "; awatingMsg = false; BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology bluetoothHelper = BluetoothHelper.GetInstance("TEST"); bluetoothHelper.setTerminatorBasedStream("\n"); Debug.Log(bluetoothHelper.getDeviceName()); msg.text += "Device name: " + bluetoothHelper.getDeviceName() + " "; bluetoothHelper.OnConnected += () => { msg.text += "Connected "; awatingMsg = false; bluetoothHelper.StartListening(); }; bluetoothHelper.OnConnectionFailed += () => { msg.text += "Connection failed "; }; bluetoothHelper.OnScanEnded += OnScanEnded; bluetoothHelper.OnDataReceived += BluetoothHelper_OnDataReceived; BluetoothHelperCharacteristic txC = new BluetoothHelperCharacteristic(UUID_TX); txC.setService(UUID); BluetoothHelperCharacteristic rxC = new BluetoothHelperCharacteristic(UUID_RX); rxC.setService(UUID); bluetoothHelper.setRxCharacteristic(rxC); bluetoothHelper.setTxCharacteristic(txC); bluetoothHelper.ScanNearbyDevices(); } catch (Exception ex) { exception.text += ex + " "; } } }
void Start() { timer = 0; try{ Debug.Log("HI"); BluetoothHelper.BLE = true; //use Bluetooth Low Energy Technology bluetoothHelper = BluetoothHelper.GetInstance("TEST"); Debug.Log(bluetoothHelper.getDeviceName()); bluetoothHelper.OnConnected += () => { Debug.Log("Connected"); sendData(); }; bluetoothHelper.OnConnectionFailed += () => { Debug.Log("Connection failed"); }; bluetoothHelper.OnScanEnded += OnScanEnded; bluetoothHelper.OnServiceNotFound += (serviceName) => { Debug.Log(serviceName); }; bluetoothHelper.OnCharacteristicNotFound += (serviceName, characteristicName) => { Debug.Log(characteristicName); }; bluetoothHelper.OnCharacteristicChanged += (value, characteristic) => { Debug.Log(characteristic.getName()); Debug.Log(System.Text.Encoding.ASCII.GetString(value)); }; // BluetoothHelperService service = new BluetoothHelperService("FFE0"); // service.addCharacteristic(new BluetoothHelperCharacteristic("FFE1")); // BluetoothHelperService service2 = new BluetoothHelperService("180A"); // service.addCharacteristic(new BluetoothHelperCharacteristic("2A24")); // bluetoothHelper.Subscribe(service); // bluetoothHelper.Subscribe(service2); // bluetoothHelper.ScanNearbyDevices(); BluetoothHelperService service = new BluetoothHelperService("19B10000-E8F2-537E-4F6C-D104768A1214"); service.addCharacteristic(new BluetoothHelperCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214")); bluetoothHelper.Subscribe(service); //bluetoothHelper.Subscribe(service2); bluetoothHelper.ScanNearbyDevices(); }catch (Exception ex) { Debug.Log(ex.Message); } }