File tree Expand file tree Collapse file tree 4 files changed +60
-9
lines changed
BootstrapBlazor/Extensions Expand file tree Collapse file tree 4 files changed +60
-9
lines changed Original file line number Diff line number Diff line change @@ -21,15 +21,20 @@ private IBluetoothService? BluetoothService { get; set; }</Pre>
2121<DemoBlock Title =" @Localizer[" BaseUsageTitle " ]"
2222 Introduction =" @Localizer[" BaseUsageIntro " ]"
2323 Name =" Normal" >
24- <div class =" row form-inline" >
24+ <div class =" row form-inline g-3 " >
2525 <div class =" col-12" >
26- <Button Text =" @Localizer[" BluetoothRequestText " ]" OnClick =" RequestDevice" ></Button >
27- <Button Text =" @Localizer[" BluetoothConnectText " ]" IsDisabled =" @(_blueDevice is not {Connected: false})" OnClick =" Connect" ></Button >
28- <Button Text =" @Localizer[" BluetoothDisconnectText " ]" IsDisabled =" @(_blueDevice is not {Connected: true})" OnClick =" Disconnect" ></Button >
26+ <Button Text =" @Localizer[" BluetoothRequestText " ]" Icon = " fa-brands fa-bluetooth " OnClick =" RequestDevice" ></Button >
27+ <Button Text =" @Localizer[" BluetoothConnectText " ]" Icon = " fa-solid fa-circle-play " IsAsync = " true " IsDisabled =" @(_blueDevice is not {Connected: false})" OnClick =" Connect" class = " ms-3 " ></Button >
28+ <Button Text =" @Localizer[" BluetoothDisconnectText " ]" Icon = " fa-solid fa-circle-stop " IsDisabled =" @(_blueDevice is not {Connected: true})" OnClick =" Disconnect" class = " ms-3 " ></Button >
2929 </div >
3030 <div class =" col-12" >
31- <label >Battery: <progress value =" @_batteryValue" max =" 100" ></progress ></label >
32- <Button Text =" @Localizer[" BluetoothGetBatteryText " ]" IsDisabled =" @(_blueDevice is not {Connected: true})" OnClick =" GetBatteryValue" ></Button >
31+ <div class =" d-flex align-items-center" >
32+ <Button Text =" @Localizer[" BluetoothGetBatteryText " ]" Icon =" fa-solid fa-battery-half" IsDisabled =" @(_blueDevice is not { Connected: true })" OnClick =" GetBatteryValue" ></Button >
33+ <label class =" d-flex align-items-center" ><progress value =" @_batteryValue" max =" 100" class =" ms-3" ></progress ><span class =" ms-3" >@_batteryValueString </span ></label >
34+ </div >
35+ </div >
36+ <div class =" col-12" >
37+ <Button Text =" @Localizer[" BluetoothGetHeartRateText " ]" Icon =" fa-solid fa-heart-pulse" IsDisabled =" @(_blueDevice is not { Connected: true })" OnClick =" GetHeartRateValue" ></Button >
3338 </div >
3439 </div >
3540</DemoBlock >
Original file line number Diff line number Diff line change @@ -19,6 +19,8 @@ public partial class Bluetooth
1919
2020 private string ? _batteryValue = null ;
2121
22+ private string ? _batteryValueString = null ;
23+
2224 private async Task RequestDevice ( )
2325 {
2426 _blueDevice = await BluetoothService . RequestDevice ( [ "battery_service" ] ) ;
@@ -60,12 +62,39 @@ private async Task Disconnect()
6062
6163 private async Task GetBatteryValue ( )
6264 {
65+ _batteryValue = null ;
66+ _batteryValueString = null ;
67+
6368 if ( _blueDevice != null )
6469 {
65- _batteryValue = await _blueDevice . GetBatteryValue ( ) ;
70+ var val = await _blueDevice . GetBatteryValue ( ) ;
71+ if ( val == 0 && ! string . IsNullOrEmpty ( _blueDevice . ErrorMessage ) )
72+ {
73+ await ToastService . Error ( "Battery Value" , _blueDevice . ErrorMessage ) ;
74+ return ;
75+ }
76+
77+ _batteryValue = $ "{ val } ";
78+ _batteryValueString = $ "{ _batteryValue } %";
79+ }
80+ }
81+
82+ private async Task GetHeartRateValue ( )
83+ {
84+ if ( _blueDevice != null )
85+ {
86+ _batteryValue = null ;
87+ _batteryValueString = null ;
88+ _batteryValue = await _blueDevice . GetHeartRateValue ( ) ;
89+
6690 if ( string . IsNullOrEmpty ( _batteryValue ) && ! string . IsNullOrEmpty ( _blueDevice . ErrorMessage ) )
6791 {
6892 await ToastService . Error ( "Battery Value" , _blueDevice . ErrorMessage ) ;
93+ return ;
94+ }
95+ if ( ! string . IsNullOrEmpty ( _batteryValue ) )
96+ {
97+ _batteryValueString = $ "{ _batteryValue } %";
6998 }
7099 }
71100 }
Original file line number Diff line number Diff line change 58995899 "BluetoothConnectText" : " 连接" ,
59005900 "BluetoothDisconnectText" : " 断开" ,
59015901 "BluetoothGetBatteryText" : " 读取电量" ,
5902+ "BluetoothGetHeartRateText" : " 读取心率" ,
59025903 "BaseUsageTitle" : " 基础用法" ,
59035904 "BaseUsageIntro" : " 通过 <code>IBluetoothService</code> 服务,请求与蓝牙设备通讯" ,
59045905 "BluetoothBatteryLevelTitle" : " 蓝牙设备电量" ,
Original file line number Diff line number Diff line change @@ -14,11 +14,27 @@ public static class IBluetoothDeviceExtensions
1414 /// </summary>
1515 /// <param name="blueDevice"></param>
1616 /// <returns></returns>
17- public static async Task < string ? > GetBatteryValue ( this IBluetoothDevice blueDevice )
17+ public static async Task < byte > GetBatteryValue ( this IBluetoothDevice blueDevice )
1818 {
19- string ? value = null ;
19+ byte value = 0 ;
2020 var data = await blueDevice . ReadValue ( "battery_service" , "battery_level" ) ;
2121 if ( data is { Length : > 0 } )
22+ {
23+ value = data [ 0 ] ;
24+ }
25+ return value ;
26+ }
27+
28+ /// <summary>
29+ /// 获得 设备电量方法
30+ /// </summary>
31+ /// <param name="blueDevice"></param>
32+ /// <returns></returns>
33+ public static async Task < string ? > GetHeartRateValue ( this IBluetoothDevice blueDevice )
34+ {
35+ string ? value = null ;
36+ var data = await blueDevice . ReadValue ( "heart_rate" , "heart_rate_measurement" ) ;
37+ if ( data is { Length : > 0 } )
2238 {
2339 value = $ "{ data [ 0 ] } %";
2440 }
You can’t perform that action at this time.
0 commit comments