| | |
| | | /// <param name="FinishEvent">搜索结束的事件</param> |
| | | public void ScanBluetooth(int waitTime, Action<List<BluetoothInfo>> FinishEvent) |
| | | { |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | //再次检测是否能够搜索蓝牙 |
| | | this.CheckCanScanBluetooth((result) => |
| | | { |
| | |
| | | }); |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | { |
| | | var listBluetoothInfo = new List<BluetoothInfo>(); |
| | | |
| | | var adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter; |
| | | var scanner = adapter.BluetoothLeScanner; |
| | | BluetoothScanCallback scanCallback = null; |
| | | Android.Bluetooth.BluetoothAdapter adapter = null; |
| | | Android.Bluetooth.LE.BluetoothLeScanner scanner = null; |
| | | |
| | | var scanCallback = new BluetoothScanCallback(); |
| | | //以防万一,蓝牙都丢在主线程中运行 |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | adapter = Android.Bluetooth.BluetoothAdapter.DefaultAdapter; |
| | | scanner = adapter.BluetoothLeScanner; |
| | | |
| | | scanCallback = new BluetoothScanCallback(); |
| | | scanner.StartScan(null, new Android.Bluetooth.LE.ScanSettings.Builder().SetScanMode(Android.Bluetooth.LE.ScanMode.LowLatency).Build(), scanCallback); |
| | | }, ShowErrorMode.NO); |
| | | |
| | | //等待 |
| | | System.Threading.Thread.Sleep(waitTime * 1000); |
| | | |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | scanner.StopScan(scanCallback); |
| | | adapter.Dispose(); |
| | | |
| | |
| | | scanCallback.listData.Clear(); |
| | | |
| | | FinishEvent?.Invoke(listBluetoothInfo); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="connectEvent">因为需要对方反馈,所以使用回调(链接结果 false:连接失败 true:连接成功)</param> |
| | | public void ContectBluetooth(BluetoothInfo bluetooth, Action<bool> connectEvent) |
| | | { |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | try |
| | | { |
| | | this.nowBlufiClient = new Blufi.Espressif.BlufiClient(Application.Activity, bluetooth.Device); |
| | |
| | | connectEvent?.Invoke(false); |
| | | connectEvent = null; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | try |
| | | { |
| | | this.sendStatuValue = -1; |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | //发送数据 |
| | | var byteData = System.Text.Encoding.UTF8.GetBytes(i_data); |
| | | this.nowBlufiClient.PostCustomData(byteData); |
| | | |
| | | }, ShowErrorMode.NO); |
| | | |
| | | if (waiTime == 0) { return true; } |
| | | |
| | | waiTime *= 5; |