【最新API翻译】 Bluetooth--BluetoothAdapter API 翻译

最近学习Bluetooth通讯的问题,看了几篇文章,再看API的时候更加觉得API才是王道,因此尝试自己翻译一下,借此学习理解。

英文原文:
class overview:
Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for Bluetooth LE devices.

To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE. Fundamentally, this is your starting point for all Bluetooth actions. Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); start device discovery with startDiscovery(); or create a BluetoothServerSocket to listen for incoming connection requests with listenUsingRfcommWithServiceRecord(String, UUID); or start a scan for Bluetooth LE devices with startLeScan(LeScanCallback).Note: Most methods require the BLUETOOTH permission and some also require the BLUETOOTH_ADMIN permission.

中文翻译:

类概述:
BluetoothAdapter表示本地设备的蓝牙适配器。你可以利用BluetoothAdapter执行基本的蓝牙操作,比如发起蓝牙设备搜索,查询手机已连接(或配对)的设备列表,通过已知的MAC地址初始化一个蓝牙设备,以及生成一个BluetoothServerSocket类来监听其它设备的连接请求或者发起对其它蓝牙设备的搜索(Bluetooth LE 即 Bluetooth low energy)。

要获取到代表本机蓝牙适配器的BluetoothAdapter,在JELLY_BEAN_MR1(4.2)或者更低版本设备上,可以调用静态方法getDefaultAdapter();在JELLY_BEAN_MR2(4.3)及高版本上,调用getSystemService(String)并结合字符串常量BLUETOOTH_SERVICE。一般来说,操作蓝牙设备前都要先获取BluetoothAdapter。

一旦获取了本地的适配器,就可以通过
调用getBondedDevices()获取已配对设备集合;调用startDiscovery()开始蓝牙设备搜索;
调用 listenUsingRfcommWithServiceRecord(String, UUID)产生一个BluetoothServerSocket来监听连接请求;
调用startLeScan(LeScanCallback)扫描BLE设备。

注意:大部分的方法要求获得“BLUETOOTH”权限,一些也需要获得“BLUETOOTH_ADMIN”权限。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C# Windows 11提供的Bluetooth API实现蓝牙扫描、配对、通信的例程: 1. 打开Bluetooth 首先,你需要在你的应用程序中添加 Bluetooth 权限。在你的UWP应用程序的 Package.appxmanifest 文件中添加以下代码: ```xml <Capabilities> <Capability Name="internetClient" /> <DeviceCapability Name="bluetooth" /> </Capabilities> ``` 然后在你的代码里引入Windows.Devices.Bluetooth和Windows.Devices.Enumeration命名空间。 ```csharp using Windows.Devices.Bluetooth; using Windows.Devices.Enumeration; ``` 接下来,你需要打开蓝牙。下面是打开蓝牙的代码: ```csharp BluetoothAdapter adapter = await BluetoothAdapter.GetDefaultAsync(); if (adapter == null) { // 蓝牙未打开 } else { // 蓝牙已打开 } ``` 2. 扫描蓝牙设备 接下来,你需要扫描蓝牙设备。你需要使用 DeviceInformation.FindAllAsync() 方法来扫描所有设备,并使用 BluetoothLEDevice.FromIdAsync() 方法连接到设备。下面是扫描蓝牙设备的代码: ```csharp DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()); foreach (DeviceInformation device in devices) { BluetoothLEDevice bluetoothDevice = await BluetoothLEDevice.FromIdAsync(device.Id); // 连接到设备 } ``` 3. 配对蓝牙设备 如果你要使用蓝牙设备进行通信,你需要先配对设备。下面是配对蓝牙设备的代码: ```csharp // 连接到设备 BluetoothLEDevice bluetoothDevice = await BluetoothLEDevice.FromIdAsync(device.Id); // 配对设备 DevicePairingResult result = await bluetoothDevice.DeviceInformation.Pairing.PairAsync(); if (result.Status == DevicePairingResultStatus.Paired) { // 设备已配对 } else { // 设备配对失败 } ``` 4. 发送和接收数据 一旦你已经连接到蓝牙设备并已经配对,你可以开始发送和接收数据。你需要使用 GattCharacteristic 类来访问设备的特性,并使用 GattCharacteristic.WriteValueAsync() 方法写入数据。下面是发送和接收数据的代码: ```csharp // 连接到设备 BluetoothLEDevice bluetoothDevice = await BluetoothLEDevice.FromIdAsync(device.Id); // 获取设备的服务 GattDeviceServicesResult servicesResult = await bluetoothDevice.GetGattServicesAsync(); if (servicesResult.Status == GattCommunicationStatus.Success) { // 获取服务成功 GattDeviceService service = servicesResult.Services.FirstOrDefault(); // 获取服务的特性 GattCharacteristicsResult characteristicsResult = await service.GetCharacteristicsAsync(); if (characteristicsResult.Status == GattCommunicationStatus.Success) { // 获取特性成功 GattCharacteristic characteristic = characteristicsResult.Characteristics.FirstOrDefault(); // 写入数据 byte[] data = Encoding.UTF8.GetBytes("Hello, World!"); await characteristic.WriteValueAsync(data.AsBuffer()); // 读取数据 GattReadResult readResult = await characteristic.ReadValueAsync(); byte[] buffer = readResult.Value.ToArray(); string message = Encoding.UTF8.GetString(buffer); } } ``` 这就是使用C# Windows 11提供的Bluetooth API实现蓝牙扫描、配对、通信的例程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值