List of usage examples for android.bluetooth BluetoothDevice getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:Main.java
private static synchronized BluetoothSocket getRfcommSocketByReflection(BluetoothDevice device) throws Exception { if (device == null) return null; Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); return (BluetoothSocket) m.invoke(device, 1); }
From source file:Main.java
public static ArrayList<ParcelUuid> getDeviceUuids(BluetoothDevice device) { ArrayList<ParcelUuid> result = new ArrayList<ParcelUuid>(); try {//w ww .j a va 2s . c o m Method method = device.getClass().getMethod("getUuids", null); ParcelUuid[] phoneUuids = (ParcelUuid[]) method.invoke(device, null); if (phoneUuids != null) { for (ParcelUuid uuid : phoneUuids) { if (D) Log.d(TAG, device.getName() + ": " + uuid.toString()); result.add(uuid); } } } catch (NoSuchMethodException e) { e.printStackTrace(); if (D) Log.e(TAG, "getDeviceUuids() failed", e); } catch (InvocationTargetException e) { e.printStackTrace(); if (D) Log.e(TAG, "getDeviceUuids() failed", e); } catch (IllegalAccessException e) { e.printStackTrace(); if (D) Log.e(TAG, "getDeviceUuids() failed", e); } return result; }
From source file:Main.java
public static BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { UUID uuid = UUID.fromString(SERIAL_SERVICE_UUID); BluetoothSocket socket = null;/* ww w .j ava 2 s . co m*/ if (Build.VERSION.SDK_INT >= 10) { try { final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class }); System.out.println("Performing invoke"); socket = (BluetoothSocket) m.invoke(device, uuid); System.out.println("Invoke done"); System.out.println(socket); } catch (Exception e) { Log.e("error", "Could not create Insecure RFComm Connection", e); } } else { //TODO, support older versions with secure connection } return socket; }
From source file:com.google.android.apps.mytracks.util.Api8Adapter.java
@Override public BluetoothSocket getBluetoothSocket(BluetoothDevice bluetoothDevice) throws IOException { try {/*from w w w.j av a2s . com*/ Class<? extends BluetoothDevice> c = bluetoothDevice.getClass(); Method insecure = c.getMethod("createInsecureRfcommSocket", Integer.class); insecure.setAccessible(true); return (BluetoothSocket) insecure.invoke(bluetoothDevice, 1); } catch (SecurityException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } catch (NoSuchMethodException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } catch (IllegalArgumentException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } catch (IllegalAccessException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } catch (InvocationTargetException e) { Log.d(Constants.TAG, "Unable to create insecure connection", e); } return bluetoothDevice.createRfcommSocketToServiceRecord(BluetoothConnectionManager.MY_TRACKS_UUID); }
From source file:com.wolkabout.hexiwear.activity.MainActivity.java
@ItemLongClick(R.id.listDevices) void unbindDevice(final BluetoothDeviceWrapper wrapper) { try {// w w w . j a va 2 s . c o m final BluetoothDevice device = wrapper.getDevice(); device.getClass().getMethod("removeBond", (Class[]) null).invoke(device, (Object[]) null); Toast.makeText(this, "Device unpaired.", Toast.LENGTH_SHORT).show(); adapter.notifyDataSetChanged(); } catch (Exception e) { Log.e(TAG, "Can't remove bond", e); } }
From source file:com.dipesan.miniatm.miniatm.services.BluetoothConnexionManager.java
public boolean connect() { if (deviceAddr == null) { return false; }//from w w w.j av a 2 s.c o m LogManager.debug(BluetoothConnexionManager.class.getSimpleName(), "connect to " + deviceAddr); if (socket != null && socket.isConnected()) { try { socket.close(); } catch (IOException ignored) { } } BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddr); try { // socket = device.createRfcommSocketToServiceRecord(BT_UUID); Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] { int.class }); socket = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); socket.connect(); RPCManager.getInstance().start(socket.getInputStream(), socket.getOutputStream()); return true; } catch (Exception e) { LogManager.debug(BluetoothConnexionManager.class.getName(), "connect device error", e); if (socket != null && socket.isConnected()) { try { socket.close(); } catch (IOException ignored) { } } return false; } }
From source file:mobi.monaca.framework.plugin.BluetoothPlugin.java
/** * Execute a bluetooth function/*w w w . j a v a2 s . c o m*/ */ @Override public PluginResult execute(String action, JSONArray args, String callbackId) { PluginResult pluginResult = null; Log.d("BluetoothPlugin", "Action: " + action); if (ACTION_ENABLE.equals(action)) { // Check if bluetooth isn't disabled already if (!m_bluetoothAdapter.isEnabled()) { m_stateChanging = true; ctx.startActivityForResult(this, new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), 1); while (m_stateChanging) { } ; } // Check if bluetooth is enabled now if (m_bluetoothAdapter.isEnabled()) { pluginResult = new PluginResult(PluginResult.Status.OK); } else { pluginResult = new PluginResult(PluginResult.Status.ERROR); } } // Want to disable bluetooth? else if (ACTION_DISABLE.equals(action)) { if (!m_bluetoothAdapter.disable() && m_bluetoothAdapter.isEnabled()) { pluginResult = new PluginResult(PluginResult.Status.ERROR); } else { pluginResult = new PluginResult(PluginResult.Status.OK); } } else if (ACTION_DISCOVERDEVICES.equals(action)) { m_discoveredDevices = new JSONArray(); if (!m_bluetoothAdapter.startDiscovery()) { pluginResult = new PluginResult(PluginResult.Status.ERROR, "Unable to start discovery"); } else { m_discovering = true; // Wait for discovery to finish while (m_discovering) { } Log.d("BluetoothPlugin", "DiscoveredDevices: " + m_discoveredDevices.length()); pluginResult = new PluginResult(PluginResult.Status.OK, m_discoveredDevices); } } // Want to list UUIDs of a certain device else if (ACTION_GETUUIDS.equals(action)) { try { String address = args.getString(0); Log.d("BluetoothPlugin", "Listing UUIDs for: " + address); // Fetch UUIDs from bluetooth device BluetoothDevice bluetoothDevice = m_bluetoothAdapter.getRemoteDevice(address); Method m = bluetoothDevice.getClass().getMethod("fetchUuidsWithSdp"); Log.d("BluetoothPlugin", "Method: " + m); m.invoke(bluetoothDevice); m_gettingUuids = true; while (m_gettingUuids) { } pluginResult = new PluginResult(PluginResult.Status.OK, m_gotUUIDs); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } // Connect to a given device & uuid endpoint else if (ACTION_CONNECT.equals(action)) { try { String address = args.getString(0); UUID uuid = UUID.fromString(args.getString(1)); Log.d("BluetoothPlugin", "Connecting..."); BluetoothDevice bluetoothDevice = m_bluetoothAdapter.getRemoteDevice(address); BluetoothSocket bluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(uuid); bluetoothSocket.connect(); m_bluetoothSockets.add(bluetoothSocket); int socketId = m_bluetoothSockets.indexOf(bluetoothSocket); pluginResult = new PluginResult(PluginResult.Status.OK, socketId); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } else if (ACTION_READ.equals(action)) { try { int socketId = args.getInt(0); BluetoothSocket bluetoothSocket = m_bluetoothSockets.get(socketId); InputStream inputStream = bluetoothSocket.getInputStream(); char[] buffer = new char[128]; for (int i = 0; i < buffer.length; i++) { buffer[i] = (char) inputStream.read(); } //Log.d( "BluetoothPlugin", "Buffer: " + String.valueOf(buffer) ); pluginResult = new PluginResult(PluginResult.Status.OK, String.valueOf(buffer)); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } else if (ACTION_DISCONNECT.equals(action)) { try { int socketId = args.getInt(0); // Fetch socket & close it BluetoothSocket bluetoothSocket = m_bluetoothSockets.get(socketId); bluetoothSocket.close(); // Remove socket from internal list m_bluetoothSockets.remove(socketId); // Everything went fine... pluginResult = new PluginResult(PluginResult.Status.OK); } catch (Exception e) { Log.e("BluetoothPlugin", e.toString() + " / " + e.getMessage()); pluginResult = new PluginResult(PluginResult.Status.JSON_EXCEPTION, e.getMessage()); } } return pluginResult; }
From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java
/** * ?mac???/*from w w w. j a v a 2s.co m*/ * * @param args * ???mac?json * @return PluginResult */ private PluginResult pairBluetooth(JSONArray args) { String addressDevice = null; try { addressDevice = args.getString(0); } catch (JSONException e) { e.printStackTrace(); } if (mBtadapter.isDiscovering()) { mBtadapter.cancelDiscovery(); } BluetoothDevice device = mBtadapter.getRemoteDevice(addressDevice); boolean paired = false; Method m; try { m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return new PluginResult(PluginResult.Status.OK, paired); }
From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java
/** * ?mac?????// www . j av a 2 s. c o m * * @param args * ????mac?json * @return PluginResult */ private PluginResult unpairBluetooth(JSONArray args) { String addressDevice = null; try { addressDevice = args.getString(0); } catch (JSONException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } if (mBtadapter.isDiscovering()) { mBtadapter.cancelDiscovery(); } BluetoothDevice device = mBtadapter.getRemoteDevice(addressDevice); boolean IsUnpaired = false; Method m; try { m = device.getClass().getMethod("removeBond"); IsUnpaired = (Boolean) m.invoke(device); } catch (SecurityException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } catch (NoSuchMethodException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } catch (IllegalArgumentException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } catch (IllegalAccessException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } catch (InvocationTargetException e) { e.printStackTrace(); return new PluginResult(PluginResult.Status.ERROR, e.getMessage()); } return new PluginResult(PluginResult.Status.OK, IsUnpaired); }
From source file:com.cypress.cysmart.HomePageActivity.java
private void unpairDevice(BluetoothDevice device) { try {//from ww w . j a v a 2s . c o m Method m = device.getClass().getMethod("removeBond", (Class[]) null); m.invoke(device, (Object[]) null); } catch (Exception e) { e.printStackTrace(); } }