1***@qq.com
1***@qq.com
  • 发布:2016-07-19 11:31
  • 更新:2018-04-08 23:51
  • 阅读:2104

IOS BLE开发

分类:Native.js

通过nativejs实现IOS BLE功能,运行的时候app直接假死

IOS代码如下:

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    // Create a NSUUID object
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"A77A1B68-49A7-4DBF-914C-760D07FBB87B"];

    // Initialize the Beacon Region
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
    major:1
    minor:1
    identifier:@"com.appcoda.testregion"];
    }

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (IBAction)buttonClicked:(id)sender {

    // Get the beacon data to advertise
    self.myBeaconData = [self.myBeaconRegion peripheralDataWithMeasuredPower:nil];

    // Start the peripheral manager
    self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self
    queue:nil
    options:nil];
    }

-(void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral
{
if (peripheral.state == CBPeripheralManagerStatePoweredOn)
{
// Bluetooth is on

    // Update our status label  
    self.statusLabel.text = @"Broadcasting...";  

    // Start broadcasting  
    [self.peripheralManager startAdvertising:self.myBeaconData];  
}  
else if (peripheral.state == CBPeripheralManagerStatePoweredOff)  
{  
    // Update our status label  
    self.statusLabel.text = @"Stopped";  

    // Bluetooth isn't on. Stop broadcasting  
    [self.peripheralManager stopAdvertising];  
}  
else if (peripheral.state == CBPeripheralManagerStateUnsupported)  
{  
    self.statusLabel.text = @"Unsupported";  
}  

}

@end

import <UIKit/UIKit.h>

import <CoreLocation/CoreLocation.h>

import <CoreBluetooth/CoreBluetooth.h>

@interface ViewController : UIViewController<CBPeripheralManagerDelegate>

@property (weak, nonatomic) IBOutlet UILabel statusLabel;
@property (strong, nonatomic) CLBeaconRegion
myBeaconRegion;
@property (strong, nonatomic) NSDictionary myBeaconData;
@property (strong, nonatomic) CBPeripheralManager
peripheralManager;

@end

nativejs代码如下:
var NSUUID = plus.ios.importClass("NSUUID");
var CLBeaconRegion = plus.ios.importClass("CLBeaconRegion");
var NSDictionary = plus.ios.importClass("NSDictionary");
var CLLocationManager = plus.ios.importClass("CLLocationManager");
var CLBeaconMajorValue = plus.ios.importClass("CLBeaconMajorValue");
var CLBeaconMinorValue = plus.ios.importClass("CLBeaconMinorValue");
var CBPeripheralManager = plus.ios.importClass("CBPeripheralManager");

    Major = plus.ios.newObject("CLBeaconMajorValue", 10069);  
    Minor = plus.ios.newObject("CLBeaconMinorValue", 37625);  

    //1  
    var uuid = new NSUUID();  
    uuid.initWithUUIDString("FDA50693-A4E2-4FB1-AFCF-C6EB07647825");  
    //2  
    clBeaconRegion = new CLBeaconRegion();  

// clBeaconRegion.initWithProximityUUIDmajorminoridentifier(uuid, Major, Minor, "Fmxy_xxxxx");
clBeaconRegion.initWithProximityUUIDidentifier(uuid,"test");
nsd = new NSDictionary();
nsd = clBeaconRegion.peripheralDataWithMeasuredPower(null);
cbpmd = plus.ios.implements("CoreBluetooth.ICBPeripheralManagerDelegate", {
"peripheralManagerDidUpdateState": function(peripheral) {
console.log("CBPeripheralManagerStatePoweredOn");
}
});

    cbpm=new CBPeripheralManager();  
    cbpm.initWithDelegatequeue(cbpmd,null);   
    cbpm.startAdvertising(nsd);                

请大神帮忙看看~

2016-07-19 11:31 负责人:无 分享
已邀请:
1***@qq.com

1***@qq.com (作者)

"frameworks": [
"CoreBluetooth.framework",
"CoreLocation.framework",
"CoreGraphics.framework",
"UIKit.framework",
"Foundation.framework"
]

1***@qq.com

1***@qq.com (作者)

坐等答复

KyleFu

KyleFu

cbpmd = plus.ios.implements("CoreBluetooth.ICBPeripheralManagerDelegate", {
"peripheralManagerDidUpdateState": function(peripheral) {
console.log("CBPeripheralManagerStatePoweredOn");
}
});

这部分内容改成

cbpmd = plus.ios.implements("CoreBluetooth.ICBPeripheralManagerDelegate", {  
"peripheralManagerDidUpdateState:": function(peripheral) {  
console.log("CBPeripheralManagerStatePoweredOn");  
}   
});

JSON对象中key值为代理方法的名称,使用":"分割参数;value值为Function,方法参数必须与代理方法中的":"区配。

2***@qq.com

2***@qq.com - 123

这个代码在ios11下App直接退出了,在ios9下正常

  • 1***@qq.com

    原来如此,我也闪退

    2018-04-08 23:50

1***@qq.com

1***@qq.com

闪退有解吗

该问题目前已经被锁定, 无法添加新回复