protected override void OnButtonClick(Button btn) { base.OnButtonClick(btn); switch (btn.name) { case "Bluetooth2StartFrameBtn": GuiController.Instance.SwitchWrapperWithScale(GuiFrameID.StartFrame, false); break; case "BluetoothCentralBtn": InitializeBluetooth(true); break; case "BluetoothPeripheralBtn": InitializeBluetooth(false); break; case "BackFromContentBtn": scaning = false; BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.RemoveServices(); BluetoothLEHardwareInterface.DeInitialize(() => { MyDebug.LogGreen("DeInitialize Success!"); }); CommonTool.GuiHorizontalMove(bluetoothCategoryContent, Screen.width, MoveID.RightOrUp, canvasGroup, false); break; case "BackFromScanResultBtn": case "BluetoothAdvertisingStopBtn": bluetoothPeripheralDetailBg.SetActive(false); bluetoothConnectWaiting.SetActive(false); bluetoothAdvertisingStopBtn.SetActive(false); StopScan(); break; case "BluetoothScanBtn": StartScan(); break; case "ConnectCancelBtn": bluetoothPeripheralDetailBg.SetActive(false); break; case "BluetoothReScanBtn": ReScan(); break; default: MyDebug.LogYellow("Can not find Button: " + btn.name); break; } }
private void InitializeBluetooth(bool isCentral) { this.isCentral = isCentral; BluetoothLEHardwareInterface.Initialize(isCentral, !isCentral, () => { MyDebug.LogGreen("Initialize Success!"); BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.RemoveServices(); bluetoothCategoryContent.SetActive(true); RefreshCategoryContent(); CommonTool.GuiHorizontalMove(bluetoothCategoryContent, Screen.width, MoveID.RightOrUp, canvasGroup, true); }, (error) => { string message = ""; if (error.Contains("Not Supported")) { MyDebug.LogYellow("Not Supported"); message = LanguageController.Instance.GetLanguage("Text_80009"); } else if (error.Contains("Not Authorized")) { MyDebug.LogYellow("Not Authorized"); message = LanguageController.Instance.GetLanguage("Text_80010"); } else if (error.Contains("Powered Off")) { MyDebug.LogYellow("Powered Off"); message = LanguageController.Instance.GetLanguage("Text_80011"); } else if (error.Contains("Not Enabled")) { MyDebug.LogYellow("Not Enabled"); message = LanguageController.Instance.GetLanguage("Text_80012"); } else { MyDebug.LogYellow("Central Initialize Fail: " + error); message = LanguageController.Instance.GetLanguage("Text_80021"); } GuiController.Instance.CurCommonTipInstance = new CommonTipInstance(CommonTipID.Single, message); GuiController.Instance.SwitchWrapper(GuiFrameID.CommonTipFrame, true); }); }
public void OnBluetoothFightFinish() { MyDebug.LogGreen("OnBluetoothFightFinish"); if (isCentral) { MyDebug.LogGreen("OnBluetoothFightFinish:Central"); BluetoothLEHardwareInterface.UnSubscribeCharacteristic(CurPeripheralInstance.address, ServiceUUID, ReadUUID, (characteristic) => { MyDebug.LogGreen("UnSubscribeCharacteristic Success :" + characteristic); }); BluetoothLEHardwareInterface.DisconnectPeripheral(CurPeripheralInstance.address, (disconnectAddress) => { MyDebug.LogGreen("DisconnectPeripheral Success:" + disconnectAddress); }); } else { MyDebug.LogGreen("OnBluetoothFightFinish:Peripheral"); BluetoothLEHardwareInterface.StopAdvertising(() => { MyDebug.LogGreen("Stop Advertising!"); }); } BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.RemoveServices(); BluetoothLEHardwareInterface.DeInitialize(() => { MyDebug.LogGreen("DeInitialize Success!"); }); BluetoothLEHardwareInterface.BluetoothEnable(false); }
public void StartClient(string networkName, string clientName, Action onStartedAdvertising, Action <string, string, byte[]> onCharacteristicWritten) { Reset(); BluetoothLEHardwareInterface.PeripheralName(networkName + ":" + clientName); BluetoothLEHardwareInterface.RemoveServices(); BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.CBCharacteristicProperties properties = BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead | BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite | BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify; BluetoothLEHardwareInterface.CBAttributePermissions permissions = BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable | BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable; BluetoothLEHardwareInterface.CreateCharacteristic(SampleCharacteristic.CharacteristicUUID, properties, permissions, null, 5, (characteristic, bytes) => { if (onCharacteristicWritten != null) { onCharacteristicWritten(clientName, characteristic, bytes); } }); BluetoothLEHardwareInterface.CreateService(SampleCharacteristic.ServiceUUID, true, (characteristic) => { StatusMessage = "Created service"; }); BluetoothLEHardwareInterface.StartAdvertising(() => { StatusMessage = "8"; if (onStartedAdvertising != null) { onStartedAdvertising(); } }); }
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); }); } }
public void OnButton(Button button) { if (button.name.Equals("ButtonCentral")) { _isCentral = true; BottomPanel.SetActive(false); SetState(States.Scan, 0.5f); } else if (button.name.Equals("ButtonPeripheral")) { _isCentral = false; BottomPanel.SetActive(false); BluetoothLEHardwareInterface.PeripheralName(DeviceName.text); BluetoothLEHardwareInterface.RemoveServices(); BluetoothLEHardwareInterface.RemoveCharacteristics(); BluetoothLEHardwareInterface.CBCharacteristicProperties properties = BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyRead | BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyWrite | BluetoothLEHardwareInterface.CBCharacteristicProperties.CBCharacteristicPropertyNotify; BluetoothLEHardwareInterface.CBAttributePermissions permissions = BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsReadable | BluetoothLEHardwareInterface.CBAttributePermissions.CBAttributePermissionsWriteable; BluetoothLEHardwareInterface.CreateCharacteristic(SampleCharacteristic.CharacteristicUUID, properties, permissions, null, 5, (characteristicWritten, bytes) => { ValueInputField.text = Encoding.UTF8.GetString(bytes); }); BluetoothLEHardwareInterface.CreateService(SampleCharacteristic.ServiceUUID, true, (characteristic) => { StatusMessage = "Created service"; }); BluetoothLEHardwareInterface.StartAdvertising(() => { MiddlePanel.SetActive(true); }); } else if (button.name.Equals("ButtonSend")) { if (_isCentral) { BluetoothLEHardwareInterface.WriteCharacteristic(_deviceAddress, SampleCharacteristic.ServiceUUID, SampleCharacteristic.CharacteristicUUID, Encoding.UTF8.GetBytes(ValueInputField.text), ValueInputField.text.Length, true, (characteristicWrite) => { }); } else { BluetoothLEHardwareInterface.UpdateCharacteristicValue(SampleCharacteristic.CharacteristicUUID, Encoding.UTF8.GetBytes(ValueInputField.text), ValueInputField.text.Length); } } else if (button.name.Equals("ButtonStop")) { if (_isCentral) { SetState(States.Disconnect, 0.5f); } else { BluetoothLEHardwareInterface.StopAdvertising(() => { Reset(); }); } } }