My company's project is a medical project, so I've been dealing with Bluetooth for a while now. I'm using CoreBluetooth, Apple's native framework, and I've encountered a few pitfalls in interfacing with a few Bluetooth devices, which I'll list below. There is a library BabyBluetooth on git that encapsulates the native CoreBluetooth framework and is easy to use, so you can try it out. So let's get started!
**
It should be noted that the following UUIDs are the Service and Characteristic UUIDs of my Bluetooth device, so be careful to replace them with the Service and Characteristic UUIDs provided by your own Bluetooth
**
**
**
There are a few issues worth noting here:
**
1. Bluetooth broadcast messages in ios usually contain the following 4 types of information. ios does not accept any other types of broadcast messages in its Bluetooth communication protocol. Therefore, it is important to note that if you need to uniquely identify a device by its Mac address when scanning for devices, you need to communicate with the hardware engineer of the Bluetooth device: put the required Mac address into the following types of broadcast messages. Usually it is placed in the field kCBAdvDataManufacturerData.
kCBAdvDataIsConnectable = 1;
kCBAdvDataLocalName = XXXXXXX;
kCBAdvDataManufacturerData = <XXXXXXXX>;
kCBAdvDataTxPowerLevel = 0;
2. The device's UUID (peripheral.identifier) is obtained by an algorithm from the macs of the two devices, so different phones connecting to the same device have different UUIDs, which cannot identify the device.
3. When Apple connects and communicates with a Bluetooth device, it is not the Mac address of the Apple Bluetooth module that is used, it is the randomly generated hexadecimal code of Apple that is used as the Mac of the phone's Bluetooth to interact with the peripheral Bluetooth device. If the Bluetooth device communicates with the cell phone several times within a certain period of time, then the randomly generated hexadecimal code is used as the Mac address when connecting for the first time, and the cell phone will clear the Mac address that has been randomly generated and regenerate it again when it exceeds this fixed period of time. That is to say that peripheral devices are not able to use the Bluetooth Mac address obtained during interaction with the Apple phone as a unique identifier for the phone. (This is based on the phenomenon of the problem speculated during the coordination with the firmware engineer who wrote the Bluetooth device. As for whether the underlying layer of Apple's Bluetooth communication protocol is indeed exactly as I said, I hope readers who know about it can help. Thanks in advance.)
It should be noted that here is the kCGMCharacteristicOneUUID feature to write, the reason why this operation is because of my Bluetooth device's Bluetooth protocol is defined in this way, so do not copy here, according to your Bluetooth device's communication protocol to determine which feature to read, which feature to write, and set Notify on which feature
It's important to note that the Bluetooth information is read here (but not in the return value, but in the
callback method)