//handles Connect //connect the device as a peripheral, and set up the characteristics for I/O properly public void OnConnect(string dName, string dAddress) { _connectedName = (string)dName.Clone(); _connectedAddress = (string)dAddress.Clone(); if (!_connecting) { //perform a connection only if it is not doing it if (_connected) { //if already connected, disconnect it if (_connectedAddress != null) { BluetoothLEHardwareInterface.DisconnectPeripheral(_connectedAddress, null); } _connected = false; } else { _readFound = false; _writeFound = false; //connect to the device with the address provided BluetoothLEHardwareInterface.ConnectToPeripheral(_connectedAddress, (address) => {//gets called when the connection is successful _connectedAddress = address; _connected = true; _connecting = false; _bleManager.IndicateConnected(dName, dAddress); //let the manager know it is connected //stop scanning if a connection is established BluetoothLEHardwareInterface.StopScan(); }, (address, serviceUUID) => {//gets called for each service the device supports BluetoothLEHardwareInterface.Log(_connectedName + " supports service: " + serviceUUID); }, (address, serviceUUID, characteristicUUID) => {//gets called for each characteristic the device supports BluetoothLEHardwareInterface.Log(serviceUUID + " supports characteristic: " + characteristicUUID); if (serviceUUID.ToUpper().CompareTo(_serviceUUID.ToUpper()) == 0) { //the _serviceUUID is a hardware-specific value indicating a certain characteristic, // i.e., different from hardware to hardware if (characteristicUUID.ToUpper().CompareTo(_characteristicUUID.ToUpper()) == 0) { _writeFound = true; //write characteristic of Bluno? } } }, (address) => { // this will get called when the device disconnects // be aware that this will also get called when the disconnect // is called above. both methods get call for the same action // this is for backwards compatibility _connected = false; } ); _connecting = true; } } }
//handles Connect //connect the device as a peripheral, and set up the characteristics for I/O properly public void OnConnect(string dName, string dAddress) { _connectedName = (string)dName.Clone(); _connectedAddress = (string)dAddress.Clone(); if (!_connecting) { //perform a connection only if it is not doing it if (_connected) { //if already connected, disconnect it if (_connectedAddress != null) { BluetoothLEHardwareInterface.DisconnectPeripheral(_connectedAddress, null); } _connected = false; } else { _readFound = false; _writeFound = false; //connect to the device with the address provided BluetoothLEHardwareInterface.ConnectToPeripheral(_connectedAddress, (address) => {//gets called when the connection is successful _connectedAddress = address; _connected = true; _connecting = false; _bleManager.IndicateConnected(dName, dAddress); //let the manager know it is connected //stop scanning if a connection is established BluetoothLEHardwareInterface.StopScan(); }, (address, serviceUUID) => {//gets called for each service the device supports BluetoothLEHardwareInterface.Log(_connectedName + " supports service: " + serviceUUID); }, (address, serviceUUID, characteristicUUID) => {//gets called for each characteristic the device service supports BluetoothLEHardwareInterface.Log(serviceUUID + " supports characteristic: " + characteristicUUID); if (serviceUUID.ToUpper().CompareTo(_serviceUUID.ToUpper()) == 0) { //the _serviceUUID is a hardware-specific value indicating a certain collection of characteristics, // i.e., different from hardware to hardware if (characteristicUUID.ToUpper().CompareTo(_characteristicUUID.ToUpper()) == 0) { //Bean has 5 characteristics for 2-way communication, this code uses the first one as write _writeFound = true; } else if (characteristicUUID.ToUpper().CompareTo(_characteristicUUID2.ToUpper()) == 0) { //Bean has 5 charateristics for 2-way communication, this code uses the second one as read _readFound = true; //subscribe to the charateristic so the code will be notified if a change happens BluetoothLEHardwareInterface.SubscribeCharacteristic(_connectedAddress, _serviceUUID, _characteristicUUID2, (theOtherCharacteristicUUID) => { //gets called when a notification occurs }, (theOtherCharacteristicUUID, values) => { //gets called when the charateristic value is updated by the peripheral, provides access to the data (values) _bleManager.IndicateDataReceived(theOtherCharacteristicUUID, values); } ); } } }, (address) => { // this will get called when the device disconnects // be aware that this will also get called when the disconnect // is called above. both methods get call for the same action // this is for backwards compatibility _connected = false; } ); _connecting = true; } } }