简体   繁体   中英

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. 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

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?

I'm searching the same thing and found this one:

React Native version of BluetoothSerial plugin. For both android and ios https://github.com/rusel1989/react-native-bluetooth-serial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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