Skip to content

update handle getExtras String in onActivityResult #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
buildscript {
repositories {
google()
jcenter { url "http://jcenter.bintray.com/" }
maven {url "http://repo.spring.io/plugins-release/"}
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

Expand Down Expand Up @@ -326,58 +326,64 @@ private void cancelDisCovery() {

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
Intent intent = this.getCurrentActivity().getIntent();

BluetoothAdapter adapter = this.getBluetoothAdapter();
Log.d(TAG, "onActivityResult " + resultCode);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE: {
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK) {
// Get the device MAC address
String address = data.getExtras().getString(
EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
if (adapter!=null && BluetoothAdapter.checkBluetoothAddress(address)) {
BluetoothDevice device = adapter
.getRemoteDevice(address);
// Attempt to connect to the device
mService.connect(device);
if(data != null){
switch (requestCode) {
case REQUEST_CONNECT_DEVICE: {
// When DeviceListActivity returns with a device to connect
if (resultCode == Activity.RESULT_OK && intent != null) {
// Get the device MAC address
// String address = data.getExtras().getString(
// EXTRA_DEVICE_ADDRESS);
String address = intent.getStringExtra(EXTRA_DEVICE_ADDRESS);
// Get the BLuetoothDevice object
if (adapter!=null && BluetoothAdapter.checkBluetoothAddress(address)) {
BluetoothDevice device = adapter
.getRemoteDevice(address);
// Attempt to connect to the device
mService.connect(device);
}
}
break;
}
break;
}
case REQUEST_ENABLE_BT: {
Promise promise = promiseMap.remove(PROMISE_ENABLE_BT);
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK && promise != null) {
// Bluetooth is now enabled, so set up a session
if(adapter!=null){
WritableArray pairedDeivce =Arguments.createArray();
Set<BluetoothDevice> boundDevices = adapter.getBondedDevices();
for (BluetoothDevice d : boundDevices) {
try {
JSONObject obj = new JSONObject();
obj.put("name", d.getName());
obj.put("address", d.getAddress());
pairedDeivce.pushString(obj.toString());
} catch (Exception e) {
//ignore.
case REQUEST_ENABLE_BT: {
Promise promise = promiseMap.remove(PROMISE_ENABLE_BT);
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK && promise != null) {
// Bluetooth is now enabled, so set up a session
if(adapter!=null){
WritableArray pairedDeivce =Arguments.createArray();
Set<BluetoothDevice> boundDevices = adapter.getBondedDevices();
for (BluetoothDevice d : boundDevices) {
try {
JSONObject obj = new JSONObject();
obj.put("name", d.getName());
obj.put("address", d.getAddress());
pairedDeivce.pushString(obj.toString());
} catch (Exception e) {
//ignore.
}
}
promise.resolve(pairedDeivce);
} else {
promise.resolve(null);
}
promise.resolve(pairedDeivce);
} else {
promise.resolve(null);
}

} else {
// User did not enable Bluetooth or an error occured
Log.d(TAG, "BT not enabled");
if (promise != null) {
promise.reject("ERR", new Exception("BT NOT ENABLED"));
} else {
// User did not enable Bluetooth or an error occured
Log.d(TAG, "BT not enabled");
if (promise != null) {
promise.reject("ERR", new Exception("BT NOT ENABLED"));
}
}
break;
}
break;
}
}

}

@Override
Expand Down