简体   繁体   English

如何在react-native中创建Android蓝牙原生模块或者是否有任何插件随时可用

[英]How to create Android bluetooth native module in react-native or Is there any plugin readily available

I am new to react-native. 我是反应原生的新手。 I am working on application which connects to a bluetooth device and gets data from it. 我正在开发应用程序,它连接到蓝牙设备并从中获取数据。 As React native allows to create native modules and use them in React JS, I am trying to create a native bluetooth module. 由于React本机允许创建本机模块并在React JS中使用它,我正在尝试创建本机蓝牙模块。 Could any one help me out. 任何人都可以帮助我。

/**
* Created by ravitheja.bandari on 4/4/2016.
 */
public class BluetoothRN extends ReactContextBaseJavaModule implements ActivityEventListener {


// note that webView.isPaused() is not Xwalk compatible, so tracking it     poor-man style
private boolean isPaused;
private BluetoothAdapter bluetoothAdapter;
private Set<BluetoothDevice> pairedDevices;
private Promise bluetoothPromise;

private static final int BLUETOOTH_ENABLE_REQUEST = 1;
private static final String E_ACTIVITY_DOES_NOT_EXIST = "E_ACTIVITY_DOES_NOT_EXIST";
private static final String E_FAILED_TO_ENABLE_BLUETOOTH = "E_FAILED_TO_ENABLE_BLUETOOTH";


public BluetoothRN(ReactApplicationContext reactContext) {
super(reactContext);
}

@Override
public String getName() {
return "BluetoothAndroid";
}



@ReactMethod
public void getAdapter(){
bluetoothAdapter =   BluetoothAdapter.getDefaultAdapter();
}

@ReactMethod
public void getBondedDevices(final Promise promise){

Activity currentActivity = getCurrentActivity();

if (currentActivity == null) {
    promise.reject(E_ACTIVITY_DOES_NOT_EXIST, "Activity doesn't exist");
    return;
}

// Store the promise to resolve/reject when picker returns data
bluetoothPromise = promise;

try {
    Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);

    currentActivity.startActivityForResult(turnOn, BLUETOOTH_ENABLE_REQUEST);
} catch (Exception e) {
    bluetoothPromise.reject(E_FAILED_TO_ENABLE_BLUETOOTH, e);
    bluetoothPromise = null;
}


}



private ArrayList getPairedDevices(){
pairedDevices = bluetoothAdapter.getBondedDevices();
ArrayList list = new ArrayList();

for(BluetoothDevice bt : pairedDevices)
    list.add(bt.getName());

return list;
}

@Nullable
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();

return constants;
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

}
}

ReactPackage ReactPackage

public class BluetoothReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {

List<NativeModule> modules = new ArrayList<>();

modules.add(new BluetoothRN(reactContext));


return modules;
}

@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return null;
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return null;
}
}

re: Is there any plugin readily available? re:有没有随时可用的插件?

I'm searching the same thing and found this one: 我正在寻找相同的东西并找到了这个:

React Native version of BluetoothSerial plugin. React Native版本的BluetoothSerial插件。 For both android and ios https://github.com/rusel1989/react-native-bluetooth-serial 对于android和ios https://github.com/ruse​​l1989/react-native-bluetooth-serial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM