是否可以将iOS 7设备作为蓝牙LE外设(iBeacon)运行并在后台进行广告宣传?我已经能够使用下面的代码在前台做广告,并且可以从另一个iOS设备看到它但是一旦我回到主屏幕就停止广告.我确实在plist中添加了蓝牙外设背景模式,但这似乎没有帮助,虽然我得到提示说该设备想要在后台使用蓝牙.我做错了什么,或者这在iOS 7中是不可能的?
peripManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
NSString *identifier = @"MyBeacon";
//Construct the region
CLBeaconRegion *beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid identifier:identifier];
//Passing nil will use the device default power
NSDictionary *payload = [beaconRegion peripheralDataWithMeasuredPower:nil];
//Start advertising
[peripManager startAdvertising:payload];
}
Run Code Online (Sandbox Code Playgroud)
这是接收/收听端的代码:
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons
inRegion:(CLBeaconRegion *)region
{
//Check if we have moved closer or farther away from the iBeacon…
if (beacons.count > 0) {
CLBeacon *beacon …Run Code Online (Sandbox Code Playgroud) 我正在使用这个BLE插件构建一个带有Xamarin的iOS应用程序:
https://github.com/aritchie/bluetoothle
我只是通过BLE播放UUID,它可以工作.这是我的代码:
var data = new Plugin.BluetoothLE.Server.AdvertisementData
{
LocalName = "MyServer",
};
data.ServiceUuids.Add(new Guid("MY_UUID_HERE"));
await this.server.Start(data);
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,一旦我将应用程序放在后台,它就会停止广播.当我再次打开应用程序时再次恢复.
一旦它在后台,我怎么能让它继续播放?我在这里阅读文档:
它说我必须使用CBCentralManager该类来获取保存和恢复功能(所以我可以随时继续广播UUID),但我很难将其转换为Xamarin/C#.
编辑
在研究了一些之后,我读到我需要在委托中创建一个实例CBCentralManager并实现WillRestoreState它.我是这样做的AppDelegate:
[Register("AppDelegate")]
public class AppDelegate : MvxApplicationDelegate, ICBCentralManagerDelegate
{
private IGattServer server = CrossBleAdapter.Current.CreateGattServer();
private CBCentralManager cbCentralManager;
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// irrelevant code...
this.Ble();
return true;
}
private async Task Ble()
{
try
{
await Task.Delay(5000); // wait for it to finish initializing so I …Run Code Online (Sandbox Code Playgroud)