bool IsEqual(string uuid1, string uuid2) { if (uuid1.Length == 4) { uuid1 = BluetoothLEHardwareInterface.FullUUID(uuid1); } if (uuid2.Length == 4) { uuid2 = BluetoothLEHardwareInterface.FullUUID(uuid2); } return(uuid1.ToUpper().CompareTo(uuid2.ToUpper()) == 0); }
// public GUISkin skin; // // void OnGUI(){ // GUI.skin = skin; // GUIStyle myButtonStyle = new GUIStyle(GUI.skin.button); // myButtonStyle.fontSize = 20; // GUI.Button (new Rect (10, 0, 300, 100), string.Format ("sta:{0} id:{1}", State, deviceAddress), myButtonStyle); // GUI.Button (new Rect (10, 0, 300, 200), string.Format ("xor:{0}", BitConverter.ToString(addressXor)), myButtonStyle); // GUI.Button (new Rect (10, 0, 300, 300), string.Format ("data:{0}", lastData.ToString()), myButtonStyle); // GUI.Button (new Rect (10, 0, 300, 400), string.Format ("timeout:{0}", timeout), myButtonStyle); // } // Update is called once per frame void Update() { if (state != States.Subscribe) { if (restarttime <= Time.realtimeSinceStartup) { Disconnect(deviceAddress); } } if (timeout != 0 && timeout <= Time.realtimeSinceStartup) { timeout = 0; switch (state) { case States.None: break; case States.Init: StartProcess(); break; case States.Scan: Time.timeScale = 0; Alert.Show("Can't fine the ArGun, Scaning...", null, false); // TODO 这里暂停事件 BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(new string[] { BluetoothLEHardwareInterface.FullUUID(broadcastUUID) }, (_address, _name) => { if (!illegalDevices.Contains(_address)) { BluetoothLEHardwareInterface.StopScan(); deviceAddress = _address; Alert.Show("Connectting the ar gun : " + _name, null, false); SetState(States.Connect, 0.2f); } }); break; case States.Connect: BluetoothLEHardwareInterface.ConnectToPeripheral(deviceAddress, null, null, (_address, _serviceUUID, _characteristicUUID) => { if (IsEqual(serviceUUID, _serviceUUID) && IsEqual(characteristicUUID, _characteristicUUID)) { #if UNITY_ANDROID string[] addressArr = _address.Split(':'); for (int i = 0; i < addressArr.Length; i++) { addressXor [i] = (byte)(Convert.ToByte(addressArr [i], 16) ^ saveTable [i]); } #endif Alert.Show("connect success!", (_value) => { SetState(States.Subscribe, 1f); }); } }, (_address) => { SetState(States.Scan, 0.5f); }); break; case States.Subscribe: // TODO 这里恢复事件 Time.timeScale = 1f; BluetoothLEHardwareInterface.SubscribeCharacteristicWithDeviceAddress(deviceAddress, serviceUUID, characteristicUUID, null, (_address, _characteristicUUID, _bytes) => { if (_bytes.Length == addressXor.Length + 1) { #if UNITY_ANDROID for (int i = 0; i < addressXor.Length; i++) { if (!_bytes [i].Equals(addressXor [i])) { Disconnect(_address); return; } } #endif ReadData(_bytes [addressXor.Length]); } else { Disconnect(_address); } }); break; } } }
private void StartScan() { if (scaning) { MyDebug.LogYellow("Scaning!!!"); return; } scaning = true; BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.RemoveServices(); CategoryInstance curCategoryInstance = new CategoryInstance(curPatternID, curAmountID, curSymbolID, curDigitID, curOperandID); FightController.Instance.CurCategoryInstance = curCategoryInstance; string serviceUUID = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "0"; string readUUID = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "1"; string writeUUID = (int)curAmountID + "" + (int)curSymbolID + "" + (int)curDigitID + "2"; BluetoothController.Instance.ServiceUUID = BluetoothLEHardwareInterface.FullUUID(serviceUUID); BluetoothController.Instance.ReadUUID = BluetoothLEHardwareInterface.FullUUID(readUUID); BluetoothController.Instance.WriteUUID = BluetoothLEHardwareInterface.FullUUID(writeUUID); MyDebug.LogGreen("ServiceUUID:" + BluetoothController.Instance.ServiceUUID); MyDebug.LogGreen("ReadUUID:" + BluetoothController.Instance.ReadUUID); MyDebug.LogGreen("WriteUUID:" + BluetoothController.Instance.WriteUUID); if (isCentral) { MyDebug.LogGreen("Central Start Scaning!"); bluetoothScanResultContent.SetActive(true); RefreshScanResultContent(); CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, true); BluetoothLEHardwareInterface.ScanForPeripheralsWithServices(new string[] { BluetoothController.Instance.ServiceUUID }, (address, name) => { AddPeripheral(address, name); }); } else { MyDebug.LogGreen("Peripheral Start Scaning!"); BluetoothLEHardwareInterface.PeripheralName(GameManager.Instance.UserName); MyDebug.LogGreen("PeripheralName:" + GameManager.Instance.UserName); BluetoothLEHardwareInterface.CreateCharacteristic(BluetoothController.Instance.ReadUUID, BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead | BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify, BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable, null, 0, null); MyDebug.LogGreen("CreateCharacteristic:Read!"); BluetoothLEHardwareInterface.CreateCharacteristic(BluetoothController.Instance.WriteUUID, BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite, BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable, null, 0, BluetoothController.Instance.PeripheralReceiveMessage); MyDebug.LogGreen("CreateCharacteristic:Write!"); BluetoothLEHardwareInterface.CreateService(BluetoothController.Instance.ServiceUUID, true, (message) => { MyDebug.LogGreen("Create Service Success:" + message); }); MyDebug.LogGreen("CreateService!"); BluetoothLEHardwareInterface.StartAdvertising(() => { MyDebug.LogGreen("Start Advertising!"); bluetoothScanResultContent.SetActive(true); RefreshScanResultContent(); StartCoroutine(AdvertisingCountDown()); CommonTool.GuiHorizontalMove(bluetoothScanResultContent, Screen.width, MoveID.RightOrUp, canvasGroup, true); }); } }