我必须通过ble将一些广告数据发送到iOS中央应用程序。在Android外围设备方面,当我创建广告时,我会这样做:
var byteArrayData = dataToSend.toByteArray(Charsets.UTF_8)
var data = AdvertiseData.Builder().apply {
addServiceData(
ParcelUuid(MY_SERVICE_UUID),
byteArrayData
)
}
bleAdvertiser.startAdvertising(mySettings.build(), data.build(), myAdvertiseCallback)
,但似乎iOS无法读取自定义服务数据。
那么,有一种方法可以通过BLE向iOS发送一些小数据(少量字节)(并在Android为中心而iOS为外围设备时也接收数据)?
如果可能的话,我会避免在中央外围设备之间打开连接。
答案 0 :(得分:1)
iOS对广告数据有严格的限制。无论是发送还是接收它,您都只能控制它的一小部分。它的大部分由iOS本身控制,并且在中央管理者的情况下甚至没有转发给应用程序。
Advertisement Data Retrieval Keys例外,适用于centralManager(_:didDiscover:advertisementData:rssi:)的 advertisementData 参数。
此answer中提到了一个更具体的示例。
更新
即使其中一个键用于服务数据,我也不认为数据会转发到应用程序。但是我可能是错的。我想您是在问这个问题,因为未设置密钥CBAdvertisementDataServiceDataKey
。
更新2
我创建了一个最小的Android和iOS示例,并使它可以正常工作。我认为您的Android代码中没有明显的问题。因此,您需要与您的iOS同事交谈...
服务数据为“ ABC”(或十六进制的61 62 63
),而16位UUID为FF01
。 iOS日志输出为:
2019-09-05 16:39:18.987142+0200 BleScanner[18568:3982403] [Scanner] Advertisement data: FF01: <616263>
Android-MainActivity.kt
package bleadvertiser
import android.bluetooth.BluetoothManager
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
private var peripheral: Peripheral? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
peripheral = Peripheral(bluetoothManager.adapter)
peripheral?.startAdvertising()
}
}
Android-Peripheral.kt
package bleadvertiser
import android.bluetooth.BluetoothAdapter
import android.bluetooth.le.AdvertiseCallback
import android.bluetooth.le.AdvertiseData
import android.bluetooth.le.AdvertiseSettings
import android.os.ParcelUuid
import android.util.Log
import java.util.*
private const val TAG = "Peripheral"
class Peripheral(private val bluetoothAdapter: BluetoothAdapter) {
fun startAdvertising() {
val advertiseSettings = AdvertiseSettings.Builder().build()
val serviceData = "abc".toByteArray(Charsets.UTF_8)
val advertiseData = AdvertiseData.Builder()
.addServiceData(ParcelUuid(SERVICE_UUID), serviceData)
.build()
val advertiser = bluetoothAdapter.bluetoothLeAdvertiser
advertiser.startAdvertising(advertiseSettings, advertiseData, advertiseCallback)
}
private val advertiseCallback = object: AdvertiseCallback() {
override fun onStartFailure(errorCode: Int) {
Log.w(TAG, String.format("Advertisement failure (code %d)", errorCode))
}
override fun onStartSuccess(settingsInEffect: AdvertiseSettings?) {
Log.i(TAG, "Advertisement started")
}
}
companion object {
val SERVICE_UUID: UUID = UUID.fromString("0000ff01-0000-1000-8000-00805F9B34FB")
}
}
iOS-ViewController.swift
import UIKit
class ViewController: UIViewController {
var bleScanner: BleScanner?
override func viewDidLoad() {
super.viewDidLoad()
bleScanner = BleScanner()
}
}
iOS-BleScanner.swift
import Foundation
import CoreBluetooth
import os.log
class BleScanner : NSObject {
static let serviceUUID = CBUUID(string: "FF01")
static let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Scanner")
private var centralManager: CBCentralManager!
private var scanningTimer: Timer?
override init() {
super.init()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
func startScanning() {
scanningTimer = Timer.scheduledTimer(withTimeInterval: TimeInterval(20), repeats: false, block: { (_) in
self.stopScanning()
})
centralManager.scanForPeripherals(withServices: [ BleScanner.serviceUUID ], options: nil)
}
func stopScanning() {
centralManager.stopScan()
}
}
extension BleScanner : CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if centralManager.state == .poweredOn {
startScanning()
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
for (key, value) in advertisementData {
if key == CBAdvertisementDataServiceDataKey {
let serviceData = value as! [CBUUID : NSData]
for (uuid, data) in serviceData {
os_log("Advertisement data: %{public}s: %{public}s", log: BleScanner.log, type: .info, uuid.uuidString, data.debugDescription)
}
}
}
}
}