2017-08-21 20 views
0

我需要讓用戶啓​​用藍牙,但我不想使用每個平臺的本地方法,NativeScript中是否存在標準化的方法?如何讓用戶在NativeScript上啓用藍牙而不使用本機代碼

對於這種類型的請求應該使用哪些類,以便代碼與iOS和Android平臺兼容,或者它們可能提供什麼?

我需要這樣的iOS和Android代碼:

DemoAppModel.prototype.doEnableBluetooth = function() { 
    bluetooth.enable().then(function(enabled) { 
    if(!enabled) 
     setTimeout(function() { 
    /* I need this code for iOS and Android */ 
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT); 
     }, 500); 
    }); 
    }; 

回答

1

你可以使用nativescript藍牙插件,並使用the method shown in the demo app

bluetooth.isBluetoothEnabled().then(function(enabled) { 
    // show an alert dialog or some nicer UI, if 'enabled' if false 
}); 

順便說一句,在Android上,你甚至可以編程啓用通過使用this method of the plugin在設備上的藍牙(它會詢問用戶的同意):

bluetooth.enable().then(function(enabled) { /* .. */ }); 
+0

嗨@ Eddy-Verbruggen,冷杉所有的插件感謝,非常好,但不適用於藍牙經典。 我更新了問題。 – Delfino