bluetooth怎么使用_终极方法:在20分钟内使用硬件构建Bluetooth Swift应用程序

本文教程指导如何使用Swift开发一个控制Particle Mesh RGB LED的iOS应用。首先介绍如何设置环境,包括安装Xcode和创建项目。接着讲解如何实现蓝牙连接和功能,包括扫描设备、连接、服务发现和特征发现。文章还提供了测试最小功能的步骤和注意事项,以及如何添加滑块以实时更新RGB值。最后,提供了完整代码的GitHub链接。
摘要由CSDN通过智能技术生成

bluetooth怎么使用

In a previous tutorial, you learned how to add Bluetooth to a Particle Xenon application. That way you could control the onboard RGB LED from a test app like nRF Connect or Light Blue Explorer.

上一教程中 ,您学习了如何将Bluetooth添加到Particle Xenon应用程序。 这样,您可以通过nRF Connect或Light Blue Explorer等测试应用程序控制板载RGB LED。

In this post, we're going to take it one step further. We're going to develop a Swift app to control a Particle Mesh RGB led. If all goes well, you should have a working app in about 20 minutes!

在本文中,我们将更进一步。 我们将开发一个Swift应用来控制“粒子网格” RGB LED。 如果一切顺利,您应该在20分钟左右拥有一个可运行的应用程序!

Let's get started.

让我们开始吧。

现在没有时间阅读全文吗? (Don't have time right now to read the full article?)

Download the PDF version here.

在此处下载PDF版本。

设定 (Getting set up)

创建项目 (Create the project)

Once everything is installed, let's get to the fun stuff!

安装完所有内容后,让我们开始有趣的事情!

Open Xcode and go to File → New Project.

打开Xcode并转到文件→新建项目。

Select Single View App.

选择单视图应用程序。

Then update the Project Name to be to your liking. I've also changed my organization identifier to com.jaredwolff. Modify it as you see fit!

然后根据您的喜好更新项目名称 。 我还将组织标识符更改为com.jaredwolff 。 视需要修改它!

Select a location to save it.

选择一个保存位置。

Next find your Info.plist.

接下来找到您的Info.plist。

Update info.plist by adding Privacy - Bluetooth Peripheral Usage Description

通过添加Privacy - Bluetooth Peripheral Usage Description更新info.plist Privacy - Bluetooth Peripheral Usage Description

The description I ended up using was App uses Bluetooth to connect to the Particle Xenon RGB Example

我最终使用的描述是App uses Bluetooth to connect to the Particle Xenon RGB Example

This allows you to use Bluetooth in your app if you ever want to release it.

如果您想释放蓝牙,则可以在应用程序中使用蓝牙。

Now, let's get everything minimally functional!

现在,让我们让所有功能都最小化!

最小功能 (Minimally functional)

Next, we'll get a minimally functional app to connect and do a services discovery. Most of the action will happen in the ViewController.swift.

接下来,我们将获得功能最少的应用程序,以进行连接并进行服务发现。 大多数动作将发生在ViewController.swift

Lets first import CoreBluetooth

首先让我们导入CoreBluetooth

import CoreBluetooth

This allows us to control the Bluetooth Low Energy functionality in iOS. Then let's add both the CBPeripheralDelegate and CBCentralManagerDelegate to the ViewController class.

这使我们能够控制iOS中的Bluetooth Low Energy功能。 然后,将CBPeripheralDelegateCBCentralManagerDelegate添加到ViewController类。

class ViewController: UIViewController, CBPeripheralDelegate, CBCentralManagerDelegate {

Let's now create local private variables to store the actual central manager and peripheral. We'll set them up further momentarily.

现在让我们创建局部私有变量来存储实际的中央管理器和外围设备。 我们将立即对其进行进一步设置。

// Properties
    private var centralManager: CBCentralManager!
    private var peripheral: CBPeripheral!

In your viewDidLoad function, let's init the centralManager

viewDidLoad函数中,让我们初始化centralManager

centralManager = CBCentralManager(delegate: self, queue: nil)

Setting delegate: self is important. Otherwise the central state never changes on startup.

设置delegate: self很重要。 否则,启动时中央状态永远不会改变。

Before we get further, let's create a separate file and call it ParticlePeripheral.swift. It can be placed anywhere but I placed it in a separate 'group' called Models for later.

在进一步介绍之前,让我们创建一个单独的文件并将其命名为ParticlePeripheral.swift 。 它可以放置在任何地方,但我将其放在称为模型的单独“组”中,以备后用。

Inside we'll create some public variables which contain the UUIDs for our Particle Board. They should look familiar!

在内部,我们将创建一些公共变量,其中包含刨花板的UUID。 他们应该看起来很熟悉!

import UIKit
    import CoreBluetooth

    class ParticlePeripheral: NSObject {

        /// MARK: - Particle LED services and charcteristics Identifiers

        public static let particleLEDServiceUUID     = CBUUID.init(string: "b4250400-fb4b-4746-b2b0-93f0e61122c6")
        public static let redLEDCharacteristicUUID   = CBUUID.init(string: "b4250401-fb4b-4746-b2b0-93f0e61122c6")
        public static let greenLEDCharacteristicUUID = CBUUID.init(string: "b4250402-fb4b-4746-b2b0-93f0e61122c6")
        public static let blueLEDCharacteristicUUID  = CBUUID.init(string: "b4250403-fb4b-4746-b2b0-93f0e61122c6")

    }

Back in ViewController.swift let's piece together the Bluetooth bits.

回到ViewController.swift让我们将蓝牙部分拼凑起来。

蓝牙位 (Bluetooth bits)

Everything to do with Bluetooth is event based. We'll be defining several functions that handle these events. Here are the important ones:

与蓝牙有关的一切都是基于事件的。 我们将定义几个处理这些事件的函数。 这是重要的:

centralManagerDidUpdateState updates when the Bluetooth Peripheral is switched on or off. It will fire when an app first starts so you know the state of Bluetooth. We also start scanning here.

当蓝牙外围设备打开或关闭时, centralManagerDidUpdateState更新。 当应用首次启动时它将触发,因此您可以了解蓝牙的状态。 我们也从这里开始扫描。

The centralManager didDiscover event occurs when you receive scan results. We'll use this to start a connection.

当您收到扫描结果时,将发生centralManager didDiscover事件。 我们将使用它来启动连接。

The centralManager didConnect event fires once the device is connected. We'll start the device discovery here. Note: Device discovery is the way we determine what services and characteristics are available. This is a good way to confirm what type of device we're connected to.

连接设备后,就会触发centralManager didConnect事件。 我们将在这里开始设备发现。 注意:设备发现是我们确定可用服务和特征的方法。 这是确认我们连接的设备类型的好方法。

The peripheral didDiscoverServices event first once all the services have been discovered. Notice that we've switched from centralManager to peripheral now that we're connected. We'll start the characteristic discovery here. We'll be using the RGB service UUID as the target.

一旦发现所有服务, peripheral didDiscoverServices事件就会首先发生。 注意,现在我们已经连接,我们已经从centralManager切换到peripheral 。 我们将在这里开始特征发现。 我们将使用RGB服务UUID作为目标。

The peripheral didDiscoverCharacteristicsFor event will provide all the characteristics using the provided service UUID. This is the last step in the chain of doing a full device discovery. It's hairy but it only has to be done once during the connection phase!

peripheral didDiscoverCharacteristicsFor事件将使用提供的服务UUID提供所有特征。 这是执行完整设备发现链的最后一步。 这很麻烦,但是在连接阶段只需完成一次!

定义所有蓝牙功能。 (Defining all the Bluetooth functions.)

Now that we know what the functions events that get triggered. We'll define them in the logical order that they happen during a connection cycle.

现在我们知道触发了哪些函数事件。 我们将按照在连接周期内发生的逻辑顺序来定义它们。

First, we'll define centralManagerDidUpdateState to start scanning for a device with our Particle RGB LED Service. If Bluetooth is not enabled, it will not do anything.

首先,我们将定义centralManagerDidUpdateState以开始使用我们的粒子RGB LED服务扫描设备。 如果未启用蓝牙,它将不会执行任何操作。

// If we're powered on, start scanning
        func centralManagerDidUpdateState(_ central: CBCentralManager) {
            print("Central state update")
            if central.state != .poweredOn {
                print("Central is not powered on")
            } else {
                print("Central scanning for", ParticlePeripheral.particleLEDServiceUUID);
                centralManager.scanForPeripherals(withServices: [ParticlePeripheral.particleLEDServiceUUID],
                                                  options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
            }
        }

Defining the centralManager didDiscover is our next step in the process. We know we've found a device if this event has occurred.

定义centralManager didDiscover是该过程的下一步。 我们知道如果发生了此事件,我们已经找到了一个设备。

// Handles the result of the scan
        func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

            // We've found it so stop scan
            self.centralManager.stopScan()

            // Copy the peripheral instance
            self.peripheral = peripheral
            self.peripheral.delegate = self

            // Connect!
            self.centralManager.connect(self.peripheral, options: nil)

        }

So, we stop scanning using self.centralManager.stopScan(). We set the peripheral so it persists through the app. Then we connect to that device using self.centralManager.connect

因此,我们停止使用self.centralManager.stopScan()扫描。 我们设置了peripheral ,使其在应用程序中持续存在。 然后,我们使用self.centralManager.connect连接到该设备

Once connected, we need to double check if we're working with the right device.

连接后,我们需要仔细检查是否在使用正确的设备。

// The handler if we do connect succesfully
        func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
            if peripheral == self.peripheral {
                print("Connected to your Particle Board")
                peripheral.discoverServices([ParticlePeripheral.particleLEDServiceUUID])
            }
        }

By comparing the two peripherals we'll know its the device we found earlier. We'll kick off a services discovery using peripheral.discoverService. We can use ParticlePeripheral.particleLEDServiceUUID as a parameter. That way we don't pick up any services we don't care about.

通过比较两个外设,我们将知道它是我们之前发现的设备。 我们将使用peripheral.discoverService启动服务发现。 我们可以使用ParticlePeripheral.particleLEDServiceUUID作为参数。 这样一来,我们就不会再使用我们不关心的任何服务。

Once we finish the discovering services, we'll get a didDiscoverServices event. We iterate through all the "available" services. (Though there will only be one!)

完成发现服务后,我们将获得didDiscoverServices事件。 我们遍历所有“可用”服务。 (尽管只有一个!)

// Handles discovery event
        func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
            if let services = peripheral.services {
                for service in services {
                    if service.uuid == ParticlePeripheral.particleLEDServiceUUID {
                        print("LED service found")
                        //Now kick off discovery of characteristics
                        peripheral.discoverCharacteristics([ParticlePeripheral.redLEDCharacteristicUUID,
                                                                 ParticlePeripheral.greenLEDCharacteristicUUID,
                                                                 ParticlePeripheral.blueLEDCharacteristicUUID], for: service)
                        return
                    }
                }
            }
        }

By this point this is the third time we're checking to make sure we have the correct service. This becomes more handy later when there are many characteristics and many services.

至此,这是我们第三次检查以确保我们具有正确的服务。 当具有许多特征和许多服务时,这在以后变得更加方便。

We call peripheral.discoverCharacteristics with an array of UUIDs for the characteristics we're looking for. They're all the UUIDs that we defined in ParticlePeripheral.swift.

我们使用具有UUID数组的peripheral.discoverCharacteristics来寻找所需的特征。 它们都是我们在ParticlePeripheral.swift定义的UUID。

Finally, we handle the didDiscoverCharacteriscsFor event. We iterate through all the available characteristics. As we iterate we compare with the ones we're looking for.

最后,我们处理didDiscoverCharacteriscsFor事件。 我们迭代所有可用的特征。 在迭代过程中,我们将与正在寻找的对象进行比较。

// Handling discovery of characteristics
        func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
            if let characteristics = service.characteristics {
                for characteristic in characteristics {
                    if characteristic.uuid == ParticlePeripheral.redLEDCharacteristicUUID {
                        print("Red LED characteristic found")
                    } else if characteristic.uuid == ParticlePeripheral.greenLEDCharacteristicUUID {
                        print("Green LED characteristic found")
                    } else if characteristic.uuid == ParticlePeripheral.blueLEDCharacteristicUUID {
                        print("Blue LED characteristic found");
                    }
                }
            }
        }

At this point we're ready to do a full device discovery of our Particle Mesh device. In the next section we'll test what we have to make sure things are working ok.

此时,我们准备对我们的Particle Mesh设备进行完整的设备发现。 在下一节中,我们将测试必须执行的操作以确保一切正常。

测试我们的最小示例 (Testing our minimal example)

Before we get started, if you run into trouble I've put some troubleshooting steps in the footnotes.

在开始之前,如果您遇到麻烦,我已在脚注中列出了一些故障排除步骤。

To test, you'll have to have an iPhone with Bluetooth Low Energy. Most modern iPhones have it. The last iPhone not to have it I believe was either the iPhone 4 or 3Gs. (so you're likely good)

要进行测试,您必须拥有一部具有低功耗蓝牙功能的iPhone。 大多数现代iPhone都有它。 我相信最后一款没有它的iPhone是iPhone 4或3G。 (所以您可能很好)

First, plug it into your computer.

首先,将其插入计算机。

Go to the top by the play and stop buttons. Select your target device. In my case I chose my phone (Jared's iPhone). You can also use an iPad.

通过播放和停止按钮转到顶部。 选择目标设备。 就我而言,我选择了我的手机( 贾里德的iPhone )。 您也可以使用iPad。

Then you can hit Command + R or hit that Play button to load the app to your phone.

然后,您可以按Command + R按“播放”按钮以将应用程序加载到手机上。

Make sure you have your log tab open. Enable it by clicking the bottom pane button in the top right corner.

确保打开了日志选项卡。 通过单击右上角的底部窗格按钮启用它。

Make sure you have a mesh device setup and running the example code. You can go to this post to get it. Remember your Particle Mesh board needs to be running device OS 1.3.0 or greater for Bluetooth to work!

确保您具有网状设备设置并运行示例代码。 您可以转到这篇文章 。 请记住,您的Particle Mesh板需要运行设备OS 1.3.0或更高版本,蓝牙才能工作!

Once both the firmware and app is loaded, let's check the log output.

加载固件和应用程序后,让我们检查日志输出。

It should look something like this:

它看起来应该像这样:

View loaded
Central state update
Central scanning for B4250400-FB4B-4746-B2B0-93F0E61122C6
Connected to your Particle Board
LED service found
Red LED characteristic found
Green LED characteristic found
Blue LED characteristic found

This means that your Phone has connected, found the LED service! The characteristics also being discovered is important here. Without those we wouldn't be able to send data to the mesh device.

这意味着您的手机已连接,找到了LED服务! 在这里发现的特征也很重要。 没有这些,我们将无法将数据发送到网状设备。

Next step is to create some sliders so we can update the RGB values on the fly.

下一步是创建一些滑块,以便我们可以动态更新RGB值。

向左滑动。 向右滑动。 (Slide to the left. Slide to the right.)

Next we're going to add some elements to our Main.storyboard. Open Main.storyboard and click on the View underneath View Controller.

接下来,我们Main.storyboard添加一些元素。 打开Main.storyboard并点击查看 视图控制器的下方

Then click on the Library button. (It looks like the old art Apple used for the home button)

然后单击按钮。 (看起来像苹果公司用作“主页”按钮的旧艺术品)

You'll get a pop-up with all the choices that you can insert into your app.

您会看到一个弹出窗口,其中包含您可以插入应用程序中的所有选项。

Drag three Labels and copy three Sliders to your view.

拖动三个标签,然后将三个滑块复制到视图中。

You can double click on the labels and rename them as you go.

您可以双击标签并在重命名时进行重命名。

If you click and hold, some handy alignment tools will popup. They'll even snap to center!

如果单击并按住,将弹出一些方便的对齐工具。 他们甚至会居中!

You can also select them all and move them together. We'll align them vertically and horizontally.

您也可以选择它们并一起移动。 我们将它们垂直和水平对齐。

In order for them to stay in the middle, let's remove the autoresizing property. Click the Ruler icon on the top right. Then click each of the red bars. This will ensure that your labels and sliders stay on the screen!

为了让它们停留在中间,让我们删除自动调整大小属性。 单击右上角的标尺图标 。 然后单击每个红色条 。 这样可以确保您的标签和滑块在屏幕上!

Next let's click the Show Assistant Editor button. (Looks like a Venn diagram)

接下来,我们单击“ 显示助手编辑器”按钮。 (看起来像维恩图)

Note: make sure that ViewController.swift is open in your Assistant Editor.

注意:确保在“助理编辑器”中打开ViewController.swift

Then underneath the /properties section, Control-click and drag the Red Slider into your code.

然后在/properties部分下, 按住Control键并单击并将 Red Slider拖到您的代码中。

Repeat with all the other ones. Make sure you name them something different. Your code should look like this when you're done:

与其他所有重复。 确保为它们命名不同。 完成后,您的代码应如下所示:

// Properties
        private var centralManager: CBCentralManager!
        private var peripheral: CBPeripheral!

        // Sliders
        @IBOutlet weak var redSlider: UISlider!
        @IBOutlet weak var greenSlider: UISlider!
        @IBOutlet weak var blueSlider: UISlider!

This allow us to access the value of the sliders.

这使我们可以访问滑块的值。

Next, let's attach the Value Changed event to each of the sliders. Right click on the Red Slider in the folder view.

接下来,让我们将Value Changed事件附加到每个滑块。 右键单击 文件夹视图中红色滑块。

It should give you some options for events. Click and drag the Value Changed event to your code. Make sure you name it something that makes sense. I used RedSliderChanged for the Red Slider.

它应该给您一些事件选择。 单击并将Value Changed事件拖到您的代码中。 确保命名合理。 我为Red Slider使用RedSliderChanged

Repeat two more times. Your code should look like this at the end of this step:

再重复两次。 在此步骤的结尾,您的代码应如下所示:

@IBAction func RedSliderChanged(_ sender: Any) {
        }

        @IBAction func GreenSliderChanged(_ sender: Any) {
        }

        @IBAction func BlueSliderChanged(_ sender: Any) {
        }

I've also selected each of the sliders to and un-checked Enabled. That way you can't move them. We'll enable them later on in code.

我还选择了每个滑块,并取消选中Enabled 。 这样就无法移动它们。 我们稍后将在代码中启用它们。

Also, this is a great time to change the maximum value to 255. Also set the default value from 0.5 to 0.

另外,这是将最大值更改为255的好时机。 还将默认设置为0.5到0。

Back at the top of the file. Let's create some local variables for each of the characteristics. We'll use these so we can write the slider variables to the Particle Mesh board.

返回文件顶部。 让我们为每个特征创建一些局部变量。 我们将使用它们,以便将滑块变量写入“粒子网格”板。

// Characteristics
        private var redChar: CBCharacteristic?
        private var greenChar: CBCharacteristic?
        private var blueChar: CBCharacteristic?

Now, let's tie everything together!

现在,让我们将所有内容捆绑在一起!

In the didDiscoverCharacteristicsFor callback function. Let's assign those characteristics. For example

didDiscoverCharacteristicsFor回调函数中。 让我们分配这些特征。 例如

if characteristic.uuid == ParticlePeripheral.redLEDCharacteristicUUID {
        print("Red LED characteristic found")
        redChar = characteristic

As we find each characteristic, we can also enable each of the sliders in the same spot.

找到每个特征后,我们还可以在同一位置启用每个滑块。

// Unmask red slider
    		redSlider.isEnabled = true

In the end your didDiscoverCharacteristicsFor should look like:

最后,您的didDiscoverCharacteristicsFor应该看起来像:

// Handling discovery of characteristics
        func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
            if let characteristics = service.characteristics {
                for characteristic in characteristics {
                    if characteristic.uuid == ParticlePeripheral.redLEDCharacteristicUUID {
                        print("Red LED characteristic found")

                        redChar = characteristic
                        redSlider.isEnabled = true
                    } else if characteristic.uuid == ParticlePeripheral.greenLEDCharacteristicUUID {
                        print("Green LED characteristic found")

                        greenChar = characteristic
                        greenSlider.isEnabled = true
                    } else if characteristic.uuid == ParticlePeripheral.blueLEDCharacteristicUUID {
                        print("Blue LED characteristic found");

                        blueChar = characteristic
                        blueSlider.isEnabled = true
                    }
                }
            }
        }

Now, let's update the RedSliderChanged GreenSliderChanged and BlueSliderChanged functions. What we want to do here is update the characteristic associated with the Changed function. I created a separate function called writeLEDValueToChar. We'll pass in the characteristic and the data.

现在,让我们更新RedSliderChanged GreenSliderChangedBlueSliderChanged函数。 我们在这里要做的是更新与Changed函数关联的特性。 我创建了一个单独的名为writeLEDValueToChar函数。 我们将传递特性和数据。

private func writeLEDValueToChar( withCharacteristic characteristic: CBCharacteristic, withValue value: Data) {

            // Check if it has the write property
            if characteristic.properties.contains(.writeWithoutResponse) && peripheral != nil {

                peripheral.writeValue(value, for: characteristic, type: .withoutResponse)

            }

        }

Now add a call to writeLEDValueToChar to each of the Changed functions. You will have to cast the value to a Uint8. (The Particle Mesh device expects an unsigned 8-bit number.)

现在,向每个Changed函数添加对writeLEDValueToChar的调用。 您将必须将值Uint8转换为Uint8 。 (“粒子网格”设备需要一个无符号的8位数字。)

@IBAction func RedSliderChanged(_ sender: Any) {
            print("red:",redSlider.value);
            let slider:UInt8 = UInt8(redSlider.value)
            writeLEDValueToChar( withCharacteristic: redChar!, withValue: Data([slider]))

        }

Repeat this for GreenSliderChanged and BlueSliderChanged. Make sure you changed red to green and blue for each!

GreenSliderChangedBlueSliderChanged重复此BlueSliderChanged 。 确保将每种blue都从red变为greenblue

Finally, to keep things clean, i've also added a function that handles Bluetooth disconnects.

最后,为了保持环境清洁,我还添加了处理蓝牙断开连接的功能。

func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {

Inside, we should reset the state of the sliders to 0 and disable them.

在内部,我们应该将滑块的状态重置为0并禁用它们。

if peripheral == self.peripheral {
                print("Disconnected")

                redSlider.isEnabled = false
                greenSlider.isEnabled = false
                blueSlider.isEnabled = false

                redSlider.value = 0
                greenSlider.value = 0
                blueSlider.value = 0

It's a good idea to reset self.peripheral to nil that way we're not ever trying to write to a phantom device.

最好将self.peripheral重置为nil,这样我们就不会尝试写入幻像设备。

self.peripheral = nil

Finally, because we've disconnected, start scanning again!

最后,由于我们已断开连接,请重新开始扫描!

// Start scanning again
                print("Central scanning for", ParticlePeripheral.particleLEDServiceUUID);
                centralManager.scanForPeripherals(withServices: [ParticlePeripheral.particleLEDServiceUUID],
                                                  options: [CBCentralManagerScanOptionAllowDuplicatesKey : true])
            }

Alright! We just about ready to test. Let's move on to the next (and final) step.

好的! 我们即将准备测试。 让我们继续下一步(也是最后一步)。

测试滑块。 (Test the sliders.)

The hard work is done. Now it's time to play!

辛苦了。 现在是时候玩了!

The easiest way to test everything is to click the Play button in the top left or the Command + R keyboard shortcut. Xcode will load the app to your phone. You should see a white screen proceeded by a screen with your sliders!

测试所有内容的最简单方法是单击左上方的“播放”按钮Command + R键盘快捷键。 Xcode会将应用程序加载到您的手机上。 您应该会看到白色屏幕,然后是带有滑块的屏幕!

The sliders should stay greyed out until connected to your Particle Mesh board. You can check your log output if the connection has been established.

滑块应保持灰色,直到连接到您的“粒子网格”板。 您可以检查日志输出是否已建立连接。

View loaded
Central state update
Central scanning for B4250400-FB4B-4746-B2B0-93F0E61122C6
Connected to your Particle Board
LED service found
Red LED characteristic found
Green LED characteristic found
Blue LED characteristic found

(Look familiar? We're connected!)

(看起来很熟悉?我们已经连接了!)

If you followed everything perfectly, you should be able to move the sliders. Better yet, the RGB LED on the Particle Mesh board should change color.

如果您完全遵循所有步骤,则应该能够移动滑块。 更好的是,“粒子网格”板上的RGB LED应该改变颜色。

结论 (Conclusion)

In this article you've learned how to connect your Particle Mesh board and iOS device over Bluetooth. We've learned how to connect to each of the available characteristics. Plus, on top of it all, make a clean interface to do it all in.

在本文中,您已经学习了如何通过蓝牙连接Particle Mesh板和iOS设备。 我们已经学习了如何连接到每个可用特征。 另外,最重要的是,创建一个干净的界面来完成所有工作。

As you can imagine, you can go down the rabbit hole with Bluetooth on iOS. There's more coming in my upcoming guide: The Ultimate Guide to Particle Mesh. Subscribers to my list get access to pre-launch content and a discount when it comes out! Click here to get signed up.

可以想象,您可以在iOS上使用蓝牙进行操作。 我即将发布的指南还有更多内容: 《粒子网格的终极指南》。 我列表的订阅者可以访问发布前的内容,并且在发布时可以享受折扣! 单击此处进行注册。

(Code)

The full source code is available on Github. If you find it useful, hit the star button. ⭐️

完整的源代码可在Github上找到。 如果您觉得有用,请点击星号按钮。 ⭐️

翻译自: https://www.freecodecamp.org/news/ultimate-how-to-bluetooth-swift-with-hardware-in-20-minutes/

bluetooth怎么使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值