| | |
| | | /// <param name="FinishEvent">搜索结束的事件</param> |
| | | public void ScanBluetooth(int waitTime, Action<List<BluetoothInfo>> FinishEvent) |
| | | { |
| | | //再次检测是否能够搜索蓝牙 |
| | | this.CheckCanScanBluetooth((result) => |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | if (result == true) |
| | | //再次检测是否能够搜索蓝牙 |
| | | this.CheckCanScanBluetooth((result) => |
| | | { |
| | | HdlThreadLogic.Current.RunThread(() => |
| | | if (result == true) |
| | | { |
| | | //开始搜索蓝牙 |
| | | this.DoScanBluetooth(waitTime, FinishEvent); |
| | | }); |
| | | } |
| | | HdlThreadLogic.Current.RunThread(() => |
| | | { |
| | | //开始搜索蓝牙 |
| | | this.DoScanBluetooth(waitTime, FinishEvent); |
| | | }); |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | |
| | |
| | | /// <param name="connectEvent">因为需要对方反馈,所以使用回调(链接结果 false:连接失败 true:连接成功)</param> |
| | | public void ContectBluetooth(BluetoothInfo bluetooth, Action<bool> connectEvent) |
| | | { |
| | | try |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | //添加事件 |
| | | ((BlueWifiDelegate)blufiClient.BlufiDelegate).StateEvent += (div, data) => |
| | | try |
| | | { |
| | | //-1:异常 1:正常 2:发送数据成功 3:发送数据失败 |
| | | if (div == StatuEnum.A异常) |
| | | //添加事件 |
| | | ((BlueWifiDelegate)blufiClient.BlufiDelegate).StateEvent += (div, data) => |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | else if (div == StatuEnum.A正常) |
| | | { |
| | | //链接建立成功 |
| | | connectEvent?.Invoke(true); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | else if (div == StatuEnum.A发送成功 || div == StatuEnum.A发送失败) |
| | | { |
| | | sendStatuValue = div == StatuEnum.A发送成功 ? 1 : 0; |
| | | } |
| | | else if (div == StatuEnum.A蓝牙反馈) |
| | | { |
| | | //蓝牙返回的结果 |
| | | this.ReceiveEvent?.Invoke(data); |
| | | } |
| | | }; |
| | | //执行链接 |
| | | this.blufiClient.Connect(bluetooth.Address); |
| | | } |
| | | catch |
| | | { |
| | | connectEvent?.Invoke(false); |
| | | connectEvent = null; |
| | | } |
| | | //-1:异常 1:正常 2:发送数据成功 3:发送数据失败 |
| | | if (div == StatuEnum.A异常) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | else if (div == StatuEnum.A正常) |
| | | { |
| | | //链接建立成功 |
| | | connectEvent?.Invoke(true); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | else if (div == StatuEnum.A发送成功 || div == StatuEnum.A发送失败) |
| | | { |
| | | sendStatuValue = div == StatuEnum.A发送成功 ? 1 : 0; |
| | | } |
| | | else if (div == StatuEnum.A蓝牙反馈) |
| | | { |
| | | //蓝牙返回的结果 |
| | | this.ReceiveEvent?.Invoke(data); |
| | | } |
| | | }; |
| | | //执行链接 |
| | | this.blufiClient.Connect(bluetooth.Address); |
| | | } |
| | | catch |
| | | { |
| | | connectEvent?.Invoke(false); |
| | | connectEvent = null; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | #endregion |
| | |
| | | try |
| | | { |
| | | this.sendStatuValue = -1; |
| | | if (waiTime == 0) |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | //发送数据 |
| | | var data = new Foundation.NSData(); |
| | | data = i_data; |
| | | blufiClient.PostCustomData(data); |
| | | //直接返回成功 |
| | | return true; |
| | | } |
| | | else |
| | | { |
| | | //或许他用的是线程来调用 |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | //发送数据 |
| | | var data = new Foundation.NSData(); |
| | | data = i_data; |
| | | blufiClient.PostCustomData(data); |
| | | |
| | | }, ShowErrorMode.NO); |
| | | } |
| | | }, ShowErrorMode.NO); |
| | | |
| | | if (waiTime == 0) { return true; } |
| | | |
| | | waiTime *= 5; |
| | | while (this.sendStatuValue == -1 && waiTime > 0) |
| | | { |