public CharaterisInfo(string address, string serviceUUID, string characteristicUUID, CharateristicItem ITEM = null) { this.address = address; this.serviceUUID = serviceUUID; this.characteristicUUID = characteristicUUID; this.ITEM = ITEM; }
public CharateristicItem AddCharaterisItem(string address, string serviceUUID, string characteristicUUID) { //Debug.Log("AddCharaterisItem"); item_demo.gameObject.SetActive(false); CharateristicItem i = MonoBehaviour.Instantiate(item_demo); i.Init(address, serviceUUID, characteristicUUID); i.transform.SetParent(item_demo.transform.parent); i.gameObject.SetActive(true); i.transform.localScale = Vector3.one; G.Add(i.gameObject); return(i); }
private IEnumerator GetAllInfor(float waitTime) { yield return(new WaitForSeconds(waitTime)); MyLog("Starting StartCoroutine now"); { foreach (CharaterisInfo i in INFOs) { current_item = i.ITEM; i.ITEM.OnClick(); while (!i.ITEM.is_get_info) { yield return(new WaitForSeconds(0.005f)); } } } }
void Update() { if (_timeout > 0f) { _timeout -= Time.deltaTime; if (_timeout <= 0f) { _timeout = 0f; switch (_state) { case States.None: break; case States.Scan: BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(null, (address, name) => { if (!_rssiOnly) { Debug.Log("TOAN1: Scanned " + name); if (name.Contains(DeviceName)) { BluetoothLEHardwareInterface.StopScan(); // found a device with the name we want // this example does not deal with finding more than one _deviceAddress = address; SetState(States.Connect, 0.5f); } } }, (address, name, rssi, bytes) => { // use this one if the device responses with manufacturer specific data and the rssi Debug.Log("TOAN2: Scanned " + name); MyLog("Scanned " + name); if (name.Contains(DeviceName)) { if (_rssiOnly) { _rssi = rssi; } else { BluetoothLEHardwareInterface.StopScan(); _deviceAddress = address; SetState(States.Connect, 0.5f); } } }, _rssiOnly); // this last setting allows RFduino to send RSSI without having manufacturer data if (_rssiOnly) { SetState(States.ScanRSSI, 0.5f); } break; case States.ScanRSSI: break; case States.Connect: // set these flags _foundSubscribeID = false; _foundWriteID = false; // note that the first parameter is the address, not the name. I have not fixed this because // of backwards compatiblity. // also note that I am note using the first 2 callbacks. If you are not looking for specific characteristics you can use one of // the first 2, but keep in mind that the device will enumerate everything and so you will want to have a timeout // large enough that it will be finished enumerating before you try to subscribe or do any other operations. //#if UNITY_IOS // Debug.Log("TOAN: Changed address from " + _deviceAddress + " to"); // _deviceAddress = "5C:31:3E:03:5E:B7"; //#endif if (!_is_get_auto && TOGGLE_AUTO.isOn) { _is_get_auto = true; MyLog("Starting StartCoroutine after 4 seconds"); StartCoroutine(GetAllInfor(4f)); } Debug.Log("TOAN3: Trying to connect to address " + _deviceAddress); MyLog("Trying to connect to address " + _deviceAddress); BluetoothLEHardwareInterface.ConnectToPeripheral(_deviceAddress, (connectAction) => { Debug.Log("TOAN1441: Connect OK to " + connectAction); MyLog("Connected to address " + connectAction); }, (serviceAction, serviceActio2) => { Debug.Log("TOAN1442: " + serviceAction + " " + serviceActio2); MyLog("Finish getting information: " + serviceAction + " " + serviceActio2); if (_is_get_auto == false) { _is_get_auto = true; } } , (address, serviceUUID2, characteristicUUID) => { Debug.Log("TOAN1444: " + address + " ; " + serviceUUID2 + " ; " + characteristicUUID); MyLog("Found UUID: " + serviceUUID2 + " ;characteristicUUID=" + characteristicUUID); CharateristicItem i = AddCharaterisItem(address, serviceUUID2, characteristicUUID); INFOs.Add(new CharaterisInfo(address, serviceUUID2, characteristicUUID, i)); }, (s1) => { Debug.Log("TOAN1445: DISCONNECTED"); MyLog("DISCONNECTED"); }); break; case States.Subscribe: BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(_deviceAddress, ServiceUUID, SubscribeCharacteristic, null, (address, characteristicUUID, bytes) => { Debug.Log("TOAN5: SubscribeCharacteristicWithDeviceAddress to " + _deviceAddress); _state = States.None; _dataBytes = bytes; }); break; case States.Unsubscribe: Debug.Log("TOAN6: Unsubscribe to " + _deviceAddress); BluetoothLEHardwareInterface.UnSubscribeCharacteristic(_deviceAddress, ServiceUUID, SubscribeCharacteristic, null); SetState(States.Disconnect, 4f); break; case States.Disconnect: Debug.Log("TOAN7: Disconnect to " + _deviceAddress); if (_connected) { BluetoothLEHardwareInterface.DisconnectPeripheral(_deviceAddress, (address) => { BluetoothLEHardwareInterface.DeInitialize(() => { _connected = false; _state = States.None; }); }); } else { BluetoothLEHardwareInterface.DeInitialize(() => { _state = States.None; }); } break; } } } }