| | |
| | | /// <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); |
| | | }); |
| | | } |
| | | }); |
| | | }); |
| | | } |
| | | |
| | |
| | | { |
| | | 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(); |
| | | scanner.StartScan(null, new Android.Bluetooth.LE.ScanSettings.Builder().SetScanMode(Android.Bluetooth.LE.ScanMode.LowLatency).Build(), scanCallback); |
| | | //以防万一,蓝牙都丢在主线程中运行 |
| | | 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); |
| | | |
| | | scanner.StopScan(scanCallback); |
| | | adapter.Dispose(); |
| | | |
| | | foreach (var data in scanCallback.listData) |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | listBluetoothInfo.Add(data); |
| | | } |
| | | scanCallback.listData.Clear(); |
| | | scanner.StopScan(scanCallback); |
| | | adapter.Dispose(); |
| | | |
| | | FinishEvent?.Invoke(listBluetoothInfo); |
| | | foreach (var data in scanCallback.listData) |
| | | { |
| | | listBluetoothInfo.Add(data); |
| | | } |
| | | scanCallback.listData.Clear(); |
| | | |
| | | FinishEvent?.Invoke(listBluetoothInfo); |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="connectEvent">因为需要对方反馈,所以使用回调(链接结果 false:连接失败 true:连接成功)</param> |
| | | public void ContectBluetooth(BluetoothInfo bluetooth, Action<bool> connectEvent) |
| | | { |
| | | try |
| | | HdlThreadLogic.Current.RunMain(() => |
| | | { |
| | | this.nowBlufiClient = new Blufi.Espressif.BlufiClient(Application.Activity, bluetooth.Device); |
| | | |
| | | //一个回调事件 |
| | | var callback = new InnerGattCallback(); |
| | | callback.ConnectionStateEvent += (div, newState) => |
| | | try |
| | | { |
| | | if (div == 1) |
| | | this.nowBlufiClient = new Blufi.Espressif.BlufiClient(Application.Activity, bluetooth.Device); |
| | | |
| | | //一个回调事件 |
| | | var callback = new InnerGattCallback(); |
| | | callback.ConnectionStateEvent += (div, newState) => |
| | | { |
| | | if (newState == Android.Bluetooth.ProfileState.Connected) |
| | | if (div == 1) |
| | | { |
| | | //链接建立成功 |
| | | connectEvent?.Invoke(true); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | if (newState == Android.Bluetooth.ProfileState.Connected) |
| | | { |
| | | //链接建立成功 |
| | | connectEvent?.Invoke(true); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | else if (newState == Android.Bluetooth.ProfileState.Disconnected) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | } |
| | | else if (newState == Android.Bluetooth.ProfileState.Disconnected) |
| | | else if (div == -1) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | } |
| | | else if (div == -1) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | connectEvent = null; |
| | | } |
| | | }; |
| | | nowBlufiClient.SetGattCallback(callback); |
| | | }; |
| | | nowBlufiClient.SetGattCallback(callback); |
| | | |
| | | //另外一个回调事件 |
| | | var blufiCall = new BlufiCallbackMain(); |
| | | blufiCall.StateEvent += (div, data) => |
| | | //另外一个回调事件 |
| | | var blufiCall = new BlufiCallbackMain(); |
| | | blufiCall.StateEvent += (div, data) => |
| | | { |
| | | //-1:异常 1:正常 2:发送数据成功 3:发送数据失败 |
| | | if (div == StatuEnum.A异常) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | 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); |
| | | } |
| | | }; |
| | | nowBlufiClient.SetBlufiCallback(blufiCall); |
| | | //执行链接 |
| | | nowBlufiClient.Connect(); |
| | | } |
| | | catch |
| | | { |
| | | //-1:异常 1:正常 2:发送数据成功 3:发送数据失败 |
| | | if (div == StatuEnum.A异常) |
| | | { |
| | | //关闭链接 |
| | | this.DisContectBluetooth(); |
| | | connectEvent?.Invoke(false); |
| | | //只通知一次 |
| | | 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); |
| | | } |
| | | }; |
| | | nowBlufiClient.SetBlufiCallback(blufiCall); |
| | | //执行链接 |
| | | nowBlufiClient.Connect(); |
| | | } |
| | | catch |
| | | { |
| | | connectEvent?.Invoke(false); |
| | | connectEvent = null; |
| | | } |
| | | connectEvent?.Invoke(false); |
| | | connectEvent = null; |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | try |
| | | { |
| | | this.sendStatuValue = -1; |
| | | //发送数据 |
| | | var byteData = System.Text.Encoding.UTF8.GetBytes(i_data); |
| | | this.nowBlufiClient.PostCustomData(byteData); |
| | | 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; |