下面列出了android.content.Intent#getShortExtra ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
String name = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short) 0);
ScanRecordCompat record = new ScanRecordCompat();
record.setDeviceName(name);
record.setAdvertiseFlags(-1);
record.setTxPowerLevel(Integer.MIN_VALUE);
ScanResultCompat result = new ScanResultCompat();
result.setRssi(rssi);
result.setDevice(device);
result.setScanRecord(record);
Timber.d("Discovered bluetooth +" + device.getAddress() + " with name " + device.getName());
//Log.d("onReceive", "Discovered bluetooth +" + device.getAddress() + " with name " + device.getName());
if (service.addDiscoveredDevice(result)) {
service.bluetoothAdapter.cancelDiscovery();
}
}
}
public static short getShortParam(String name, short defaultValue, Fragment fragment) {
if (fragment.getArguments() != null && fragment.getArguments().containsKey(name)) {
return fragment.getArguments().getShort(name);
}
Intent i = fragment.getActivity().getIntent();
try {
Uri uri = i.getData();
if (uri != null) {
String val = uri.getQueryParameter(name);
if (!TextUtils.isEmpty(val))
return Short.parseShort(val);
}
} catch (Exception e) {
e.printStackTrace();
}
return i.getShortExtra(name, defaultValue);
}
private void onDeviceFound_classic(Context context, Intent intent)
{
// If this was discovered via the hack to show the bond popup, then do not propogate this
// any further, as this scan is JUST to get the dialog to pop up (rather than show in the notification area)
P_Task_BondPopupHack hack = m_mngr.getTaskQueue().getCurrent(P_Task_BondPopupHack.class, m_mngr);
// Only pipe discovery event if the scan task is running, and the manager says we're doing a classic scan
P_Task_Scan scan = m_mngr.getTaskQueue().getCurrent(P_Task_Scan.class, m_mngr);
if (hack == null && scan != null && m_mngr.m_config.scanApi == BleScanApi.CLASSIC)
{
final BluetoothDevice device_native = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
final P_NativeDeviceLayer layer = m_mngr.m_config.newDeviceLayer(BleDevice.NULL);
layer.setNativeDevice(device_native);
final List<P_ScanManager.DiscoveryEntry> entries = new ArrayList<>(1);
entries.add(new P_ScanManager.DiscoveryEntry(layer, rssi, null));
m_mngr.onDiscoveredFromNativeStack(entries);
}
}
private void onDeviceFound_classic(Context context, Intent intent)
{
// If this was discovered via the hack to show the bond popup, then do not propogate this
// any further, as this scan is JUST to get the dialog to pop up (rather than show in the notification area)
P_Task_BondPopupHack hack = m_mngr.getTaskQueue().getCurrent(P_Task_BondPopupHack.class, m_mngr);
// Only pipe discovery event if the scan task is running, and the manager says we're doing a classic scan
P_Task_Scan scan = m_mngr.getTaskQueue().getCurrent(P_Task_Scan.class, m_mngr);
if (hack == null && scan != null && m_mngr.m_config.scanApi == BleScanApi.CLASSIC)
{
final BluetoothDevice device_native = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
final int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
final P_NativeDeviceLayer layer = m_mngr.m_config.newDeviceLayer(BleDevice.NULL);
layer.setNativeDevice(device_native);
final List<P_ScanManager.DiscoveryEntry> entries = new ArrayList<>(1);
entries.add(new P_ScanManager.DiscoveryEntry(layer, rssi, null));
m_mngr.onDiscoveredFromNativeStack(entries);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getAddress().toLowerCase();
Log.v(TAG, "bluetooth: " + name + " => " + rssi + "dBm");
try {
bluetoothResults.put(name, rssi);
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
}
@PhonkMethod(description = "Scan bluetooth networks. Gives back the name, mac and signal strength", example = "")
@PhonkMethodParam(params = {"function(name, macAddress, strength)"})
public void scanNetworks(final ReturnInterface callbackfn) {
MLog.d(TAG, "scanNetworks");
start();
mAdapter.startDiscovery();
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
ReturnObject o = new ReturnObject();
String name = device.getName();
if (name == null) name = "";
o.put("name", name);
o.put("mac", device.getAddress());
o.put("rssi", rssi);
callbackfn.event(o);
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getContext().registerReceiver(mReceiver, filter);
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,
Short.MIN_VALUE);
SearchResult xmDevice = new SearchResult(device,
rssi, null);
notifyDeviceFounded(xmDevice);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
bluetooths.clear();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
onBluetoothChanged();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
Bluetooth bluetoothDiscovered = new Bluetooth(device.getAddress(), device.getName(), rssi);
bluetooths.add(bluetoothDiscovered);
}
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
bluetooths.clear();
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
onBluetoothChanged();
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
int rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
Bluetooth bluetoothDiscovered = new Bluetooth(device.getAddress(), device.getName(), rssi);
bluetooths.add(bluetoothDiscovered);
}
}
@Override
public void onReceive(Context context, Intent intent) {
// Read the action from the intent
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) { // Device Found
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
int rssi = (int) intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
// Create SKBluetoothDevice and add to mBluetoothDevices array
SKBluetoothDeviceData deviceData = new SKBluetoothDeviceData(System.currentTimeMillis(), name, address, rssi);
mBluetoothDevices.add(deviceData);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { // Discovery Finished
// Build the data object
SKAbstractData data = new SKBluetoothData(System.currentTimeMillis(), mBluetoothDevices);
// Clean the arrayList
mBluetoothDevices = new ArrayList<>();
// Submit sensor data object
submitSensorData(data);
// Start Discovery again
mBluetoothAdapter.startDiscovery();
}
}
@Override
public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
{
String str = paramAnonymousIntent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(str))
{
BluetoothDevice localBluetoothDevice = (BluetoothDevice) paramAnonymousIntent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//Get the hidden method for bluetooth Low Energy device categorization
int deviceClass = 0;
deviceClass = (Integer) bluetoothDeviceHiddenApi("getDeviceType", localBluetoothDevice);
if(deviceClass == 1)
{
short s = paramAnonymousIntent.getShortExtra(BluetoothDevice.EXTRA_RSSI, (short)0);
for (BluetoothDevice d: BluetoothLEStack.discoveredDevices){
if (d.getAddress().equals(localBluetoothDevice.getAddress())){
//We found the same device again- don't re-add it to the list.
return;
}
}
BluetoothLEStack.discoveredDevices.add(localBluetoothDevice);
}
}
if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(str))
{
if (BluetoothAdapter.getDefaultAdapter().isDiscovering()){
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
}
}
}
public static short getShortExtra(Intent intent, String name, short defaultValue) {
if (!hasIntent(intent) || !hasExtra(intent, name)) return defaultValue;
return intent.getShortExtra(name, defaultValue);
}
@RequiresPermission(allOf = {permission.BLUETOOTH, permission.BLUETOOTH_ADMIN})
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (bluetoothDevice == null) {
return;
}
ComparableBluetoothDevice comparableBluetoothDevice =
new ComparableBluetoothDevice(
context, bluetoothAdapter, bluetoothDevice, bluetoothDeviceActionListener);
if (BluetoothDevice.ACTION_NAME_CHANGED.equals(action)
|| BluetoothDevice.ACTION_FOUND.equals(action)) {
String bluetoothDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
comparableBluetoothDevice.setName(bluetoothDeviceName);
if (action.equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothClass bluetoothDeviceClass =
intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
comparableBluetoothDevice.setBluetoothClass(bluetoothDeviceClass);
}
/* Don't add a device if it's already been bonded (paired) to the device. */
if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
comparableBluetoothDevice.setRssi(rssi);
dispatchDeviceDiscoveredEvent(comparableBluetoothDevice);
// TODO: Remove available devices from adapter if they become
// unavailable. This will most likely be unable to be addressed without API changes.
} else {
dispatchDevicePairedEvent(comparableBluetoothDevice);
}
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED);
dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
}
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNKNOWN);
dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
}
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED);
dispatchDevicePairedEvent(comparableBluetoothDevice);
} else if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
/* The call to #createBond has completed, but the Bluetooth device isn't bonded, so
* set the connection state to unavailable. */
comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNAVAILABLE);
dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
}
/* If we canceled discovery before beginning the pairing process, resume discovery after
* {@link BluetoothDevice#createBond} finishes. */
if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDING
&& !bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.startDiscovery();
}
}
}
public static short getExtraShort(final Intent intent, final String key, final short defaultValue) {
return intent.getShortExtra(key, defaultValue);
}