List of usage examples for android.bluetooth BluetoothAdapter ACTION_DISCOVERY_STARTED
String ACTION_DISCOVERY_STARTED
To view the source code for android.bluetooth BluetoothAdapter ACTION_DISCOVERY_STARTED.
Click Source Link
From source file:Main.java
/** * register bluetooth receiver/*from w w w. ja va2 s. c om*/ * * @param receiver bluetooth broadcast receiver * @param activity activity */ public static void registerBluetoothReceiver(BroadcastReceiver receiver, Activity activity) { if (null == receiver || null == activity) { return; } IntentFilter intentFilter = new IntentFilter(); //start discovery intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); //finish discovery intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); //bluetooth status change intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); //found device intentFilter.addAction(BluetoothDevice.ACTION_FOUND); //bond status change intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED); //pairing device intentFilter.addAction("android.bluetooth.device.action.PAIRING_REQUEST"); activity.registerReceiver(receiver, intentFilter); }
From source file:com.phonegap.plugin.bluetooth.BluetoothPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { Log.d("BluetoothPlugin", "Plugin Called"); PluginResult result = null;// www .java2 s.co m context = this.ctx; // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); Looper.prepare(); btadapter = BluetoothAdapter.getDefaultAdapter(); found_devices = new ArrayList<BluetoothDevice>(); if (ACTION_DISCOVER_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES); found_devices.clear(); discovering = true; if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } Log.i("BluetoothPlugin", "Discovering devices..."); btadapter.startDiscovery(); while (discovering) { } String devicesFound = null; int count = 0; devicesFound = "["; for (BluetoothDevice device : found_devices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < found_devices.size() - 1) devicesFound = devicesFound + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } devicesFound = devicesFound + "] "; Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound); result = new PluginResult(Status.OK, devicesFound); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BT_ENABLED.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED); boolean isEnabled = btadapter.isEnabled(); Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Returning " + "is Bluetooth Enabled? " + isEnabled); result = new PluginResult(Status.OK, isEnabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_ENABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT); boolean enabled = false; Log.d("BluetoothPlugin", "Enabling Bluetooth..."); if (btadapter.isEnabled()) { enabled = true; } else { enabled = btadapter.enable(); } Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled); result = new PluginResult(Status.OK, enabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_DISABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT); boolean disabled = false; Log.d("BluetoothPlugin", "Disabling Bluetooth..."); if (btadapter.isEnabled()) { disabled = btadapter.disable(); } else { disabled = true; } Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled); result = new PluginResult(Status.OK, disabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_PAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean paired = false; Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired); result = new PluginResult(Status.OK, paired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_UNPAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean unpaired = false; Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("removeBond"); unpaired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired); result = new PluginResult(Status.OK, unpaired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES); Log.d("BluetoothPlugin", "Getting paired devices..."); Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices(); int count = 0; String resultBoundDevices = "[ "; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < pairedDevices.size() - 1) resultBoundDevices = resultBoundDevices + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } } resultBoundDevices = resultBoundDevices + "] "; Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices); result = new PluginResult(Status.OK, resultBoundDevices); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT); boolean stopped = true; Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices..."); if (btadapter.isDiscovering()) { Log.i("BluetoothPlugin", "Stop discovery..."); stopped = btadapter.cancelDiscovery(); discovering = false; } Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped); result = new PluginResult(Status.OK, stopped); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BOUND_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT); String addressDevice = arg1.getString(0); BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState()); boolean state = false; if (device != null && device.getBondState() == 12) state = true; else state = false; Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress()); Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state); result = new PluginResult(Status.OK, state); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("BluetoothPlugin", "Invalid action : " + action + " passed"); } return result; }
From source file:com.phonegap.plugin.BluetoothPlugin.java
public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); context = this.cordova.getActivity().getBaseContext(); // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); }
From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java
@Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { mCordova = cordova;/*from w w w .j ava 2 s .c om*/ try { mBtadapter = BluetoothAdapter.getDefaultAdapter(); } catch (RuntimeException e) { XLog.e(CLASS_NAME, "init: RuntimeException"); return; } mFoundDevices = new ArrayList<BluetoothDevice>(); Context context = cordova.getActivity(); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); }
From source file:org.lsc.hellocordova.BluetoothPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { Log.d("BluetoothPlugin", "Plugin Called"); PluginResult result = null;/*from w w w. jav a2 s . co m*/ context = (Context) this.ctx; // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); Looper.prepare(); btadapter = BluetoothAdapter.getDefaultAdapter(); found_devices = new ArrayList<BluetoothDevice>(); if (ACTION_DISCOVER_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES); found_devices.clear(); discovering = true; if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } Log.i("BluetoothPlugin", "Discovering devices..."); btadapter.startDiscovery(); while (discovering) { } String devicesFound = null; int count = 0; devicesFound = "["; for (BluetoothDevice device : found_devices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < found_devices.size() - 1) devicesFound = devicesFound + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } devicesFound = devicesFound + "] "; Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound); result = new PluginResult(Status.OK, devicesFound); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BT_ENABLED.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED); boolean isEnabled = btadapter.isEnabled(); Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Returning " + "is Bluetooth Enabled? " + isEnabled); result = new PluginResult(Status.OK, isEnabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_ENABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT); boolean enabled = false; Log.d("BluetoothPlugin", "Enabling Bluetooth..."); if (btadapter.isEnabled()) { enabled = true; } else { enabled = btadapter.enable(); } Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled); result = new PluginResult(Status.OK, enabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_DISABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT); boolean disabled = false; Log.d("BluetoothPlugin", "Disabling Bluetooth..."); if (btadapter.isEnabled()) { disabled = btadapter.disable(); } else { disabled = true; } Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled); result = new PluginResult(Status.OK, disabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_PAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean paired = false; Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired); result = new PluginResult(Status.OK, paired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_UNPAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean unpaired = false; Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("removeBond"); unpaired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired); result = new PluginResult(Status.OK, unpaired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES); Log.d("BluetoothPlugin", "Getting paired devices..."); Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices(); int count = 0; String resultBoundDevices = "[ "; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < pairedDevices.size() - 1) resultBoundDevices = resultBoundDevices + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } } resultBoundDevices = resultBoundDevices + "] "; Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices); result = new PluginResult(Status.OK, resultBoundDevices); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT); boolean stopped = true; Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices..."); if (btadapter.isDiscovering()) { Log.i("BluetoothPlugin", "Stop discovery..."); stopped = btadapter.cancelDiscovery(); discovering = false; } Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped); result = new PluginResult(Status.OK, stopped); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BOUND_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT); String addressDevice = arg1.getString(0); BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState()); boolean state = false; if (device != null && device.getBondState() == 12) state = true; else state = false; Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress()); Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state); result = new PluginResult(Status.OK, state); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_OPEN_SOCKET.equals(action)) { Log.d("BluetoothPlugin", "We're in " + ACTION_OPEN_SOCKET); try { String addressDevice = arg1.getString(0); openSocket(addressDevice); result = new PluginResult(Status.OK, "true"); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_OPEN_SOCKET, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_MESSAGE_SOCKET.equals(action)) { Log.d("BluetoothPlugin", "We're in " + ACTION_MESSAGE_SOCKET); try { String msg = arg1.getString(0); sendData(msg); result = new PluginResult(Status.OK, "true"); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_MESSAGE_SOCKET, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_CLOSE_SOCKET.equals(action)) { Log.d("BluetoothPlugin", "We're in " + ACTION_CLOSE_SOCKET); try { closeSocket(); result = new PluginResult(Status.OK, "true"); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_CLOSE_SOCKET, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_PING_SOCKET.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_PING_SOCKET); String data = "{"; data = data + "\"count\": " + Integer.toString(pingCount) + ","; String items = ""; for (String item : pingData) { items = items + ",\"" + item + "\""; } data = data + " \"data\" : [" + items.substring(1) + " ]"; data = data + "}"; result = new PluginResult(Status.OK, data); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_PING_SOCKET, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("BluetoothPlugin", "Invalid action : " + action + " passed"); } return result; }
From source file:cn.edu.zju.bme319.cordova.ExtraInfo.java
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { Activity activity = this.cordova.getActivity(); context = (Context) this.context; // w w w. j a v a 2 s. com // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); Looper.prepare(); found_devices = new ArrayList<BluetoothDevice>(); if (btadapter == null) { btadapter = BluetoothAdapter.getDefaultAdapter(); } if (action.equals("getExtra")) { callbackContext.success("123"); return true; } else if (ACTION_DISCOVER_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_DISCOVER_DEVICES); found_devices.clear(); discovering=true; if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } Log.i("BluetoothPlugin","Discovering devices..."); btadapter.startDiscovery(); while (discovering){} String devicesFound=null; int count=0; devicesFound="["; for (BluetoothDevice device : found_devices) { Log.i("BluetoothPlugin",device.getName() + " "+device.getAddress()+" "+device.getBondState()); if ((device.getName()!=null) && (device.getBluetoothClass()!=null)){ devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count<found_devices.size()-1) devicesFound = devicesFound + ","; }else Log.i("BluetoothPlugin",device.getName() + " Problems retrieving attributes. Device not added "); count++; } devicesFound= devicesFound + "] "; Log.d("BluetoothPlugin - "+ACTION_DISCOVER_DEVICES, "Returning: "+ devicesFound); callbackContext.success(devicesFound); //result = new PluginResult(Status.OK, devicesFound); } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_DISCOVER_DEVICES, "Got Exception "+ Ex.getMessage()); callbackContext.error("discoveryError"); //result = new PluginResult(Status.ERROR); } // try { // // Log.d("BluetoothPlugin", "We're in "+ACTION_DISCOVER_DEVICES); // // // Create a BroadcastReceiver for ACTION_FOUND //// final BroadcastReceiver mReceiver = new BroadcastReceiver() { //// public void onReceive(Context context, Intent intent) { //// String action = intent.getAction(); //// // When discovery finds a device //// if (BluetoothDevice.ACTION_FOUND.equals(action)) { //// // Get the BluetoothDevice object from the Intent //// BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); //// // Add the name and address to an array adapter to show in a ListView //// mArrayAdapter.add(device.getName() + "\n" + device.getAddress()); //// } //// } //// }; //// // Register the BroadcastReceiver //// IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); //// registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy //// //// // ?? //// if (btadapter.isDiscovering()) { //// btadapter.cancelDiscovery(); //// Log.d("BluetoothPlugin", "We're in "+"12"); //// } //// //? //// Log.d("BluetoothPlugin", "We're in "+"1234"); //// btadapter.startDiscovery(); //// Log.d("BluetoothPlugin", "We're in "+"123456"); //// //// found_devices.clear(); //// //discovering=true; //// //// //if (btadapter.isDiscovering()) { //// // btadapter.cancelDiscovery(); //// //} //// //// SendCommand(0); //// //// Log.i("BluetoothPlugin","Discovering devices..."); //// //btadapter.startDiscovery(); //// //// while (discovering){} //// //// String devicesFound=null; //// int count=0; //// devicesFound="["; //// for (BluetoothDevice device : found_devices) { //// Log.i("BluetoothPlugin",device.getName() + " "+device.getAddress()+" "+device.getBondState()); //// if ((device.getName()!=null) && (device.getBluetoothClass()!=null)){ //// devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + //// "\"address\" : \"" + device.getAddress() + "\" ," + //// "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; //// if (count<found_devices.size()-1) devicesFound = devicesFound + ","; //// }else Log.i("BluetoothPlugin",device.getName() + " Problems retrieving attributes. Device not added "); //// count++; //// } //// //// devicesFound= devicesFound + "] "; //// //// Log.d("BluetoothPlugin - "+ACTION_DISCOVER_DEVICES, "Returning: "+ devicesFound); //// callbackContext.success(devicesFound); // //result = new PluginResult(Status.OK, devicesFound); // return true; // } catch (Exception Ex) { // Log.d("BluetoothPlugin - "+ACTION_DISCOVER_DEVICES, "Got Exception "+ Ex.getMessage()); // //result = new PluginResult(Status.ERROR); // callbackContext.error("discoverError"); // return false; // } } else if (ACTION_IS_BT_ENABLED.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_IS_BT_ENABLED); boolean isEnabled = btadapter.isEnabled(); Log.d("BluetoothPlugin - "+ACTION_IS_BT_ENABLED, "Returning "+ "is Bluetooth Enabled? "+isEnabled); callbackContext.success(""+isEnabled); //result = new PluginResult(Status.OK, isEnabled); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_IS_BT_ENABLED, "Got Exception "+ Ex.getMessage()); callbackContext.error("isBTEnabledError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_ENABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_ENABLE_BT); boolean enabled = false; Log.d("BluetoothPlugin", "Enabling Bluetooth..."); if (btadapter.isEnabled()) { enabled = true; } else { enabled = btadapter.enable(); } Log.d("BluetoothPlugin - "+ACTION_ENABLE_BT, "Returning "+ "Result: "+enabled); callbackContext.success("" + enabled); //result = new PluginResult(Status.OK, enabled); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_ENABLE_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("EnableBTError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_DISABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_DISABLE_BT); boolean disabled = false; Log.d("BluetoothPlugin", "Disabling Bluetooth..."); if (btadapter.isEnabled()) { disabled = btadapter.disable(); } else { disabled = true; } Log.d("BluetoothPlugin - "+ACTION_DISABLE_BT, "Returning "+ "Result: "+disabled); callbackContext.success("" + disabled); //result = new PluginResult(Status.OK, disabled); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_DISABLE_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("DisableBTError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_PAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_PAIR_BT); String addressDevice = args.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean paired = false; Log.d("BluetoothPlugin","Pairing with Bluetooth device with name " + device.getName()+" and address "+device.getAddress()); try { Method m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - "+ACTION_PAIR_BT, "Returning "+ "Result: "+paired); callbackContext.success("" + paired); //result = new PluginResult(Status.OK, paired); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_PAIR_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("pairBTError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_UNPAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_UNPAIR_BT); String addressDevice = args.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean unpaired = false; Log.d("BluetoothPlugin","Unpairing Bluetooth device with " + device.getName()+" and address "+device.getAddress()); try { Method m = device.getClass().getMethod("removeBond"); unpaired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - "+ACTION_UNPAIR_BT, "Returning "+ "Result: "+unpaired); callbackContext.success("" + unpaired); //result = new PluginResult(Status.OK, unpaired); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_UNPAIR_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("unpairBTError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_LIST_BOUND_DEVICES); Log.d("BluetoothPlugin","Getting paired devices..."); //? Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices(); int count =0; String resultBoundDevices="[ "; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.i("BluetoothPlugin",device.getName() + " "+device.getAddress()+" "+device.getBondState()); if ((device.getName()!=null) && (device.getBluetoothClass()!=null)){ resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count<pairedDevices.size()-1) resultBoundDevices = resultBoundDevices + ","; } else Log.i("BluetoothPlugin",device.getName() + " Problems retrieving attributes. Device not added "); count++; } } resultBoundDevices= resultBoundDevices + "] "; Log.d("BluetoothPlugin - "+ACTION_LIST_BOUND_DEVICES, "Returning "+ resultBoundDevices); callbackContext.success("" + resultBoundDevices); //result = new PluginResult(Status.OK, resultBoundDevices); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_LIST_BOUND_DEVICES, "Got Exception "+ Ex.getMessage()); callbackContext.error("resultBoundDevicesError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_STOP_DISCOVERING_BT); boolean stopped = true; Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices..."); if (btadapter.isDiscovering()) { Log.i("BluetoothPlugin","Stop discovery..."); stopped = btadapter.cancelDiscovery(); discovering=false; } Log.d("BluetoothPlugin - "+ACTION_STOP_DISCOVERING_BT, "Returning "+ "Result: "+stopped); callbackContext.success("" + stopped); //result = new PluginResult(Status.OK, stopped); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_STOP_DISCOVERING_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("stoppedError"); //result = new PluginResult(Status.ERROR); return false; } } else if (ACTION_IS_BOUND_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in "+ACTION_IS_BOUND_BT); String addressDevice = args.getString(0); BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); Log.i("BluetoothPlugin","BT Device in state "+device.getBondState()); boolean state = false; if (device!=null && device.getBondState()==12) state = true; else state = false; Log.d("BluetoothPlugin","Is Bound with " + device.getName()+" - address "+device.getAddress()); Log.d("BluetoothPlugin - "+ACTION_IS_BOUND_BT, "Returning "+ "Result: "+state); callbackContext.success("" + state); //result = new PluginResult(Status.OK, state); return true; } catch (Exception Ex) { Log.d("BluetoothPlugin - "+ACTION_IS_BOUND_BT, "Got Exception "+ Ex.getMessage()); callbackContext.error("boundBTError"); //result = new PluginResult(Status.ERROR); return false; } } else if(ACTION_BT_CONNECT.equals(action)){ try { Log.d("BluetoothPlugin", "We're in "+ACTION_BT_CONNECT); deviceAddress = "8C:DE:52:99:26:23"; Log.d("BluetoothPlugin", "We're in "+deviceAddress); BluetoothDevice device = btadapter.getRemoteDevice(deviceAddress); //m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); //bluetoothSocket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); //SendCommand(0); // ??socket try { bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID .fromString(MY_UUID)); } catch (IOException e) { //Toast.makeText(this, "?", Toast.LENGTH_SHORT).show(); } bluetoothSocket.connect(); sendCommandFlag = 0; SendCommand(sendCommandFlag); if(bluetoothSocket.isConnected()) { this.isConnection = true; String str = ""; // try { inputStream = bluetoothSocket.getInputStream(); // ??? str = ""+1; } catch (IOException e) { //Toast.makeText(this, "??", Toast.LENGTH_SHORT).show(); //return; str=""+2; } if (bThread == false) { ReadThread.start(); bThread = true; str = ""+3; } else { bRun = true; str = ""+4; } //result = new PluginResult(Status.OK, str); //result.setKeepCallback(true); // callbackContext.sendPluginResult(result); callbackContext.success("" + str); } else { //result = new PluginResult(Status.OK, "failure"); callbackContext.error("Could not connect to "); } return true; } catch (Exception Ex) { // TODO: handle exception Log.d("BluetoothPlugin - "+ACTION_BT_CONNECT, "Got Exception "+ Ex.getMessage()); //result = new PluginResult(Status.ERROR); callbackContext.error("Could not connect to "); return false; } }else if(ACTION_BT_GETDATA.equals(action)){ try { Log.d("BluetoothPlugin", "We're in "+ACTION_BT_GETDATA); sendCommandFlag = 1; SendCommand(sendCommandFlag); // if (bluetoothSocket != null) { // if(bluetoothSocket.isConnected()){ // SendCommand(1); // } // } // //result = new PluginResult(Status.OK, spoValue); Log.v("Get1 ", returnBTData); while(returnBTData == "") { Log.v("Get2 ", returnBTData); } Log.v("Get3 ", returnBTData); callbackContext.success("" + returnBTData); //ReadThread.cancel(); bluetoothSocket.close(); bluetoothSocket = null; return true; } catch (Exception Ex) { // TODO: handle exception Log.d("BluetoothPlugin - "+ACTION_BT_GETDATA, "Got Exception "+ Ex.getMessage()); callbackContext.error("" + "getDataError"); //result = new PluginResult(Status.ERROR); return false; } } else { // result = new PluginResult(Status.INVALID_ACTION); // Log.d("BluetoothPlugin", "Invalid action : "+action+" passed"); // return result; callbackContext.error("" + "actionError"); } return false; }
From source file:com.snt.bt.recon.services.BcScanService.java
/** * Handle action Foo in the provided background thread with the provided * parameters./* w ww . j a v a 2 s . co m*/ */ private void handleActionStartScan() { IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(bReciever, filter, null, handler); BluetoothAdapter.getDefaultAdapter().startDiscovery(); }
From source file:com.molidt.easyandroid.bluetooth.BluetoothFragmentV4.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (!isInit) { initPlugin(_activity);/*from ww w . j a va 2 s .co m*/ } _Handler = new Handler(); //if UUID is null,using the package name to setting the default UUID if (BLUETOOTH_UUID == null) { BLUETOOTH_UUID = UUID.fromString(_activity.getPackageName()); } IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED); _activity.registerReceiver(mFoundReceiver, filter); if (isSupportBLE) { Intent gattServiceIntent = new Intent(_activity, BluetoothLeService.class); _activity.bindService(gattServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE); } }
From source file:com.commonsware.android.bluetooth.rxecho.RosterFragment.java
private void bluetoothReady() { isReady = true;/* w ww . j ava 2 s . c om*/ if (isThing()) { getActivity().startService(new Intent(getActivity(), ShoutingEchoService.class)); rxBluetooth.enableDiscoverability(getActivity(), REQUEST_ENABLE_DISCOVERY, 300); Toast.makeText(getActivity(), R.string.msg_disco, Toast.LENGTH_LONG).show(); } else { updateMenu(); initAdapter(); subs.add(rxBluetooth.observeDevices().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()).subscribe(this::addDevice)); subs.add(rxBluetooth.observeDiscovery().subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> discover.setEnabled(!BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(s)))); } }
From source file:activities.GatewayActivity.java
@Override public void onResume() { super.onResume(); // Register the BroadcastReceiver IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); IntentFilter filter3 = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); IntentFilter filter4 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy registerReceiver(mReceiver, filter2); // Don't forget to unregister during onDestroy registerReceiver(mReceiver, filter3); // Don't forget to unregister during onDestroy registerReceiver(mReceiver, filter4); // Don't forget to unregister during onDestroy }