public void OnBluetoothData(string deviceAddress, string characteristic, string base64Data) { if (base64Data != null) { byte[] bytes = System.Convert.FromBase64String(base64Data); if (bytes.Length > 0) { BluetoothLEHardwareInterface.Log("Device: " + deviceAddress + " Characteristic Received: " + characteristic); string byteString = ""; foreach (byte b in bytes) { byteString += string.Format("{0:X2}", b); } BluetoothLEHardwareInterface.Log(byteString); if (DidUpdateCharacteristicValueAction != null) { DidUpdateCharacteristicValueAction(characteristic, bytes); } if (DidUpdateCharacteristicValueWithDeviceAddressAction != null) { DidUpdateCharacteristicValueWithDeviceAddressAction(deviceAddress, characteristic, bytes); } } } }
void SendByte(byte value) { byte[] data = new byte[] { value }; BluetoothLEHardwareInterface.WriteCharacteristic(_connectedID, _serviceUUID, _writeCharacteristicUUID, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
public void Send(string message) { byte[] data = System.Text.Encoding.UTF8.GetBytes(message); BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, ServiceUUID, WriteCharacteristic, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
// What happens when song playing is done. private IEnumerator ReturnPlayer(float waitTime) { anim.SetBool("isPlaying", false); anim2.SetBool("isPulsing", true); yield return(new WaitForSeconds(waitTime)); //wait awhile before returning disc to slot canRotatePlayer = false; scroll.GetComponent <ScrollRect>().enabled = true; d.transform.SetParent(d.parent); d.parentToReturnTo = d.parent; d = null; foreach (Draggable cd in CdList) { if (cd.name != title) { cd.draggable = true; } } yield return(new WaitForSeconds(10)); if (!canRotatePlayer) { var data = Encoding.UTF8.GetBytes("z"); // stop motor BluetoothLEHardwareInterface.WriteCharacteristic(_hm10, ServiceUUID, Characteristic, data, data.Length, false, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); } }
// What happens during song play. private IEnumerator SendArduino() { anim.SetBool("isPlaying", true); anim2.SetBool("isPulsing", false); d.RetrieveFromDatabase(); yield return(new WaitForSeconds(1.0f)); //wait awhile before getting popularity score & playing song print("Playing Track: " + d.getTitle() + " of popularity " + d.getPopularity()); Song song = new Song(); song.setPopularity(d.getPopularity() + 1); // update view count song.setTitle(d.getTitle()); RestClient.Put("https://pico-86a8b.firebaseio.com/" + d.getTitle() + ".json", song); WaitForSeconds wait = new WaitForSeconds(0.5f); string[] lines = d.TextFile.text.Split('\n'); foreach (string line in lines) { if (!string.IsNullOrWhiteSpace(line)) // beat contains notes { foreach (string note in Regex.Split(line, " ")) { int key = int.Parse(note); // stream.Write(dict[key]); var data = Encoding.UTF8.GetBytes(dict[key]); BluetoothLEHardwareInterface.WriteCharacteristic(_hm10, ServiceUUID, Characteristic, data, data.Length, false, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); } } yield return(wait); //tempo of song } StartCoroutine(ReturnPlayer(1.0f)); }
public void OnScan() { if (_scanning) { BluetoothLEHardwareInterface.StopScan(); TextScanButton.text = "Start Scan"; _scanning = false; } else { RemovePeripherals(); // the first callback will only get called the first time this device is seen // this is because it gets added to a list in the BluetoothDeviceScript // after that only the second callback will get called and only if there is // advertising data available BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, name) => { AddPeripheral(name, address); }, (address, name, rssi, advertisingInfo) => { if (advertisingInfo != null) { BluetoothLEHardwareInterface.Log(string.Format("Device: {0} RSSI: {1} Data Length: {2} Bytes: {3}", name, rssi, advertisingInfo.Length, BytesToString(advertisingInfo))); } }); TextScanButton.text = "Stop Scan"; _scanning = true; } }
/// <summary> /// Initializes the BLE service and returns it's result via callbacks. If successful, you can start scanning /// for devices /// </summary> public static void Initialize(Action onSuccess, Action <string> onError, bool asCentral = true, bool asPeripheral = false) { if (IsInitialized) { Debug.LogError("BLE Service is already initialized"); return; } IsInitialized = true; BluetoothLEHardwareInterface.Initialize(asCentral, asPeripheral, () => { IsInitialized = true; SetState(State.ReadyToScan); onSuccess?.Invoke(); }, error => { BluetoothLEHardwareInterface.Log("Error: " + error); onError?.Invoke(error); }); Observable .OnceApplicationQuit() .Subscribe(unit => { DeInitialize(); }); }
public void StartScan(Callback Success, Callback Error) { m_ScanAndConnectSuccess = Success; m_ScanAndConnectError = Error; m_ConnectCharacteristicUUID = new Dictionary <string, bool>(); for (int i = 0; i < CharacteristicUUID.Length; i++) { m_ConnectCharacteristicUUID[CharacteristicUUID[i]] = false; } //Reset(); BluetoothLEHardwareInterface.Initialize(true, false, () => { SetMode(eMode.ScanDevice); }, (error) => { BluetoothLEHardwareInterface.Log("Error during initialize: " + error); if (error == "Bluetooth LE Not Enabled") { BluetoothLEHardwareInterface.DeInitialize(() => { BluetoothLEHardwareInterface.BluetoothEnable(true); if (m_ScanAndConnectError != null) { m_ScanAndConnectError(); } }); } }); }
private IEnumerator SendArduino() { yield return(new WaitForSeconds(1.0f)); //wait awhile before playing song WaitForSeconds wait = new WaitForSeconds(0.5f); string[] lines = d.TextFile.text.Split('\n'); foreach (string line in lines) { if (!string.IsNullOrWhiteSpace(line)) // beat contains notes { print(line); foreach (string note in Regex.Split(line, " ")) { int key = int.Parse(note); print("sending " + key + ": " + dict[key]); // stream.Write(dict[key]); var data = Encoding.UTF8.GetBytes(dict[key]); BluetoothLEHardwareInterface.WriteCharacteristic(_hm10, ServiceUUID, Characteristic, data, data.Length, false, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); } } yield return(wait); //tempo of song } StartCoroutine(ReturnPlayer(1.0f)); }
void SendByte(byte value) { byte[] data = new byte[] { value }; BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, ServiceUUID, WriteCharacteristic, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
public void startCalibration() { BluetoothLEHardwareInterface.Log("Start Calibrating"); calibrationAlgorithm.CurrentTrainingIndex = 0; calibrationAlgorithm.IsTrainingComplete = false; IsCalibrating = true; }
void AddPeripheral(string name, string address) { BluetoothLEHardwareInterface.Log("Found: " + name); if (_peripheralList == null) { _peripheralList = new Dictionary <string, string>(); } if (!_peripheralList.ContainsKey(address)) { _peripheralList[address] = name; if (name.Trim().ToLower() == deviceToConnectTo.Trim().ToLower()) { BluetoothLEHardwareInterface.Log("Found device"); BluetoothLEHardwareInterface.Log("Connecting to " + address); connectBluetooth(address); } else { BluetoothLEHardwareInterface.Log("Target device not founrd"); } } else { BluetoothLEHardwareInterface.Log("No device found"); } }
void connectBluetooth(string addr) { BluetoothLEHardwareInterface.ConnectToPeripheral(addr, (address) => {}, (address, serviceUUID) => {}, (address, serviceUUID, characteristicUUID) => { if (isEqual(serviceUUID, _serviceUUID)) { _connectedID = address; isConnected = true; if (isEqual(characteristicUUID, _readCharacteristicUUID)) { _readFound = true; } if (isEqual(characteristicUUID, _writeCharacteristicUUID)) { _writeFound = true; } BluetoothLEHardwareInterface.Log("Characteristic found: " + address + " -> " + serviceUUID + " -> " + characteristicUUID + "\n"); } }, (address) => { isConnected = false; }); _connecting = false; }
private void WriteCharacteristic(byte[] data) { BluetoothLEHardwareInterface.WriteCharacteristic(deviceAddress, ServiceUUID, RXUUID, data.ToArray(), data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded with characteristic: " + characteristicUUID); }); }
void sendBytesBluetooth(byte[] data) { BluetoothLEHardwareInterface.Log(string.Format("data length: {0} data {1} uuid: {2}", data.Length.ToString(), ASCIIEncoding.UTF8.GetString(data), _writeCharacteristicUUID)); BluetoothLEHardwareInterface.WriteCharacteristic(_connectedID, _serviceUUID, _writeCharacteristicUUID, data, data.Length, false, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
/// <summary> /// Connects to a device and gets its characteristic items. You can check if that device has the services + /// characteristics that you are looking for. An error may happen on the way, so check the error callback /// </summary> public static void ConnectToDevice(Device device, OnCharacteristicFound onCharacteristicFound, Action <string> onDisconnect) { CheckIfInitialized(); if (CurrentState != State.Scanning && CurrentState != State.Disconnected) { throw new Exception("You can only connect to a device after you have scanned for devices. Maybe it's not " + "true though :)"); } SetState(State.Connecting); Observable .Timer(TimeSpan.FromSeconds(0.5f)) .Subscribe(_ => { BluetoothLEHardwareInterface.ConnectToPeripheral(device.Address, null, null, (address, serviceUuid, characteristicUuid) => { SetState(State.Connected); onCharacteristicFound?.Invoke(new Characteristic { Device = device, ServiceUuid = serviceUuid, CharacteristicUuid = characteristicUuid }); }, disconnectedAddress => { SetState(State.Disconnected); BluetoothLEHardwareInterface.Log("Device disconnected: " + disconnectedAddress); onDisconnect?.Invoke(disconnectedAddress); }); }); }
private void ConnectToTargetDevice() { BluetoothLEHardwareInterface.Log("Connecting to " + DeviceName); foundTXUUID = false; foundRXUUID = false; BluetoothLEHardwareInterface.ConnectToPeripheral(deviceAddress, null, null, (address, serviceUUID, characteristicUUID) => { if (IsEqual(serviceUUID, ServiceUUID)) { BluetoothLEHardwareInterface.Log("Connected to Puck UUID: " + serviceUUID); foundTXUUID = foundTXUUID || IsEqual(characteristicUUID, TXUUID); foundRXUUID = foundRXUUID || IsEqual(characteristicUUID, RXUUID); // Make sure there is enough timeout that if the device is still enumerating other characteristics // it finishes before we try to subscribe if (foundTXUUID && foundRXUUID) { SetState(States.Subscribe, 2f); } } }, (err) => { connected = false; SetState(States.Connect, 0.5f); } ); }
public void ExecStartSensing() { byte[] data = createSendMessage(new byte[] { /*0x40, 0x01*/ 0x44, 0x01, 0x02, 0x03, 0x03, 0x07, 0x02, 0x07, 0x02, 0xFF, 0xFF }); BluetoothLEHardwareInterface.WriteCharacteristic(m_DeviceAddress, ServiceUUID, GENERAL_PURPOSE_CONTROL, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
void SendBytes(byte[] data) { BluetoothLEHardwareInterface.Log(string.Format("data length: {0} uuid: {1}", data.Length.ToString(), FullUUID(_writeCharacteristicUUID))); BluetoothLEHardwareInterface.WriteCharacteristic(_connectedID, FullUUID(_serviceUUID), FullUUID(_writeCharacteristicUUID), data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
public void SendBytes(byte[] data) { BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, ServiceUUID, TransforUUID, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
public void StartDeviceScan() { if (_scanning) { BluetoothLEHardwareInterface.Log("Stop scanning for peripherals"); BluetoothLEHardwareInterface.StopScan(); _scanning = false; } if (Connected) { BluetoothLEHardwareInterface.Log("Disconnect current peripheral"); DisconnectDevice(_connectedDevice); } // the first callback will only get called the first time this device is seen // this is because it gets added to a list in the BluetoothDeviceScript // after that only the second callback will get called and only if there is // advertising data available BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(serviceIds, (address, name) => { BluetoothLEHardwareInterface.Log("Found Device"); AddPeripheral(name, address); StopDeviceScan(); ConnectToDevice(new HRMDevice(name, address)); }, (address, name, rssi, advertisingInfo) => { }); _scanning = true; }
void SendString(string s) { byte[] data = Encoding.ASCII.GetBytes(s); print("Sending String"); BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, ServiceUUID, WriteCharacteristic, data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
void ReadString() { BluetoothLEHardwareInterface.ReadCharacteristic(_deviceAddress, ServiceUUID, ReadCharacteristic, (characteristicUUID, bytes) => { print(Encoding.ASCII.GetString(bytes)); BluetoothLEHardwareInterface.Log("read Succeeded"); }); }
//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; } } }
void ReadString() { //TODO: adapt for multiple config BluetoothLEHardwareInterface.ReadCharacteristic(_deviceAddressBlue, ServiceUUID, ReadCharacteristic, (characteristicUUID, bytes) => { print(Encoding.ASCII.GetString(bytes)); BluetoothLEHardwareInterface.Log("read Succeeded"); }); }
public void ScanButtonClick() { //Initialize(as central, as peripheral, Action, error Action) BluetoothLEHardwareInterface.Initialize(true, false, () => { SetState(States.Scan, 0.1f); }, (error) => { BluetoothLEHardwareInterface.Log("Error during initialize: " + error); }); }
//Send a byte array to BSN public static void SendBytesBSN(byte[] data, string bsnAddress, string serviceUUID, string writeCharacteristicUUID) { BluetoothLEHardwareInterface.Log(string.Format("data length: {0} uuid: {1}", data.Length.ToString(), FullBSNUUID(writeCharacteristicUUID))); BluetoothLEHardwareInterface.WriteCharacteristic(bsnAddress, FullBSNUUID(serviceUUID), FullBSNUUID(writeCharacteristicUUID), data, data.Length, true, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); Debug.Log("ESCREVEU COM SUCESSO"); }
public void StartDrag() { dragging = true; var data = Encoding.UTF8.GetBytes("w"); BluetoothLEHardwareInterface.WriteCharacteristic(_hm10, ServiceUUID, Characteristic, data, data.Length, false, (characteristicUUID) => { BluetoothLEHardwareInterface.Log("Write Succeeded"); }); }
public void StartProcess() { Reset(); BluetoothLEHardwareInterface.Initialize(true, false, () => { SetState(States.Scan, 0.1f); }, (error) => { BluetoothLEHardwareInterface.Log("Error during initialize: " + error); }); }
public GloveDataProcessor() { if (calibrationAlgorithm == null) { BluetoothLEHardwareInterface.Log("Glove address : " + GloveUuid); calibrationAlgorithm = new CalibrationAlgorithm(); } }