void connect() { try { //Log.d(MetaWatch.TAG, "got Bluetooth socket"); //if (bluetoothSocket == null) //Log.d(MetaWatch.TAG, "Bluetooth socket is null"); var addr = BluetoothAddress.Parse("d0:37:61:c4:cb:25"); var serviceClass = BluetoothService.SerialPort; var ep = new BluetoothEndPoint(addr, serviceClass); bluetoothAdapter.Connect(ep); inputOutputStream = bluetoothAdapter.GetStream(); connectionState = ConnectionState.CONNECTED; BtProtocol.SendRtcNow(); BtProtocol.GetDeviceType(); } catch (IOException ioexception) { // Log.d(MetaWatch.TAG, ioexception.toString()); // sendToast(ioexception.toString()); } /*catch (SecurityException e) { * Log.d(MetaWatch.TAG, e.toString()); * } catch (NoSuchMethodException e) { * Log.d(MetaWatch.TAG, e.toString()); * } catch (IllegalArgumentException e) { * Log.d(MetaWatch.TAG, e.toString()); * } catch (IllegalAccessException e) { * Log.d(MetaWatch.TAG, e.toString()); * } catch (InvocationTargetException e) { * Log.d(MetaWatch.TAG, e.toString()); * }*/ }
private void Button1Click(object sender, RoutedEventArgs e) { var addr = BluetoothAddress.Parse("d0:37:61:c4:cb:25"); var serviceClass = BluetoothService.SerialPort; var ep = new BluetoothEndPoint(addr, serviceClass); _bluetoothClient = new BluetoothClient(); _bluetoothClient.Connect(ep); // Stream peerStream = cli.GetStream(); BtProtocol.OutStream = _bluetoothClient.GetStream(); MetaWatchService.inputOutputStream = BtProtocol.OutStream; BtProtocol.SendRtcNow(); BtProtocol.GetDeviceType(); BtProtocol.OutStream.Flush(); _readThread = new Thread(() => { while (true) { BtProtocol.OutStream.ReadTimeout = 2000000; var startByte = BtProtocol.OutStream.ReadByte(); if (startByte != 1) { continue; } var msgLen = BtProtocol.OutStream.ReadByte(); if (msgLen < 6) { MessageBox.Show("Ошибка приёма"); continue; } var msgType = BtProtocol.OutStream.ReadByte(); var msgOptions = BtProtocol.OutStream.ReadByte(); var msgDataLen = msgLen - 6; var data = new byte[msgDataLen]; BtProtocol.OutStream.Read(data, 0, msgDataLen); //CRC BtProtocol.OutStream.ReadByte(); BtProtocol.OutStream.ReadByte(); switch (msgType) { case (int)BtMessage.Message.GetDeviceTypeResponse: switch (data[0]) { case 2: Dispatcher.Invoke(new Action(delegate { sbiConnect.Content = "Подключено!"; }), System.Windows.Threading.DispatcherPriority.Normal); //MessageBox.Show("Цифровые!"); break; } break; case (int)BtMessage.Message.ButtonEventMsg: if ((data[0] & 0x1) == 0x1) { MessageBox.Show("A!"); } if ((data[0] & 0x2) == 0x2) { MessageBox.Show("B!"); } if ((data[0] & 0x4) == 0x4) { MessageBox.Show("C!"); } if ((data[0] & 0x8) == 0x8) { MessageBox.Show("D!"); } if ((data[0] & 0x20) == 0x20) { MessageBox.Show("E!"); } if ((data[0] & 0x40) == 0x40) { MessageBox.Show("F!"); } if ((data[0] & 0x80) == 0x80) { MessageBox.Show("S!"); } break; case (int)BtMessage.Message.ReadBatteryVoltageResponse: var powerGood = data[0]; var batteryCharging = data[1]; var voltage = (data[3] << 8) | data[2]; var voltageAverage = (data[5] << 8) | data[4]; MessageBox.Show(string.Format("volt:{0} avg:{1} powerGood:{2} batteryCharging: {3}", voltage / 1000f, voltageAverage / 1000f, powerGood, batteryCharging)); break; } } }); _readThread.Start(); // var buf = new byte[2]; // var readLen = peerStream.Read(buf, 0, buf.Length); // if (readLen == 2) // { // MessageBox.Show(buf[1].ToString()); // } // peerStream.Write/Read ... // //e.g. /*var buf = new byte[1000]; * var readLen = peerStream.Read(buf, 0, buf.Length); * if (readLen == 0) * { * Console.WriteLine("Connection is closed"); * } * else * * { * * Console.WriteLine("Recevied {0} bytes", readLen); * * }*/ }