private void ConnectJoy(BTDeviceInfo joy) { string addr = joy.bluetoothDeviceInfo.DeviceAddress.ToString(); JoyStick joyst = new JoyStick(addr); joy.joyStick = joyst; if (joy.joyStick.StartConnect(0)) { joy.State = deviceState.CONNECT; OnJoyStateChange?.Invoke(this.timeBoxJoyList); OnMessage?.Invoke(addr + "连接成功!"); joy.joyStick.OnReceive = buffer => { //string text = HexHelper.byteToHexStr(buffer, 18); //this.ShowMsg(text); this.OnJoyMessageReceive?.Invoke(buffer); }; //joy.joyStick.SetJoyMap(new KeyBoardJoyMap()); joy.SetJoyMap(this, joy.mapConfig); joy.joyStick.startFeed(); this.rememberMac.Remove(addr); this.rememberMac.Add(addr + ":" + joy.mapConfig.Name); FileHelper fh = new FileHelper(); fh.SaveFile("remember.txt", string.Join("\r\n", this.rememberMac)); } }
/// <summary> /// 循环检测断开的连接自动重连,以及检测手柄的连接状态 /// </summary> private void ScanJoyConnection() { while (true) { //BTDeviceInfo joy = timeBoxJoyList.Where(i => i.State == deviceState.CONNECT).FirstOrDefault(); //foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.CONNECT)) //{ // if (!joy.joyStick.CheckConnect()) // { // joy.State = deviceState.LOST; // joy.joyMap.Dispose(); // OnJoyStateChange?.Invoke(this.timeBoxJoyList); // } //} //foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.CONNECTING)) //{ // ConnectJoy(joy); //} //foreach(var joy in timeBoxJoyList.Where(i => i.State == deviceState.LOST)) //{ // ConnectJoy(joy); //} foreach (var joy in timeBoxJoyList) { switch (joy.State) { case deviceState.CONNECT: if (!joy.joyStick.CheckConnect()) { joy.State = deviceState.LOST; joy.joyMap.Dispose(); OnJoyStateChange?.Invoke(this.timeBoxJoyList); OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接已丢失!"); } break; case deviceState.CONNECTING: ConnectJoy(joy); break; case deviceState.LOST: ConnectJoy(joy); break; } } Thread.Sleep(500); } }
/// <summary> /// 连接一个手柄 /// </summary> /// <param name="index"></param> public void ConnectJoy(int index) { var joy = GetDevice(index); if (joy != null) { if (joy.State == deviceState.DISCONNECT) { joy.State = deviceState.CONNECTING; OnJoyStateChange?.Invoke(this.timeBoxJoyList); } } }
/// <summary> /// 连接一个手柄 /// </summary> /// <param name="index"></param> public void ConnectJoy(int index) { var joy = GetDevice(index); if (joy != null) { if (joy.State == deviceState.DISCONNECT) { joy.State = deviceState.CONNECTING; OnJoyStateChange?.Invoke(this.timeBoxJoyList); OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接中!"); } } }
/// <summary> /// 断连一个手柄 /// </summary> /// <param name="index"></param> public void DisconnectJoy(int index) { var joy = GetDevice(index); if (joy != null) { //if (joy.State == deviceState.CONNECT || joy.State == deviceState.CONNECTING) //{ // joy.State = deviceState.DISCONNECT; // joy.joyStick.Disconnect(); // OnJoyStateChange?.Invoke(this.timeBoxJoyList); //} joy.State = deviceState.DISCONNECT; joy.joyStick.Disconnect(); OnJoyStateChange?.Invoke(this.timeBoxJoyList); OnMessage?.Invoke(joy.bluetoothDeviceInfo.DeviceAddress.ToString() + "连接已断开!"); var addr = joy.bluetoothDeviceInfo.DeviceAddress.ToString(); this.rememberMac.Remove(addr); FileHelper fh = new FileHelper(); fh.SaveFile("remember.txt", string.Join("\r\n", this.rememberMac)); } }