本文整理汇总了Java中android.bluetooth.BluetoothDevice类的典型用法代码示例。如果您正苦于以下问题:Java BluetoothDevice类的具体用法?Java BluetoothDevice怎么用?Java BluetoothDevice使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
BluetoothDevice类属于android.bluetooth包,在下文中一共展示了BluetoothDevice类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findBluetoothDevice
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
void findBluetoothDevice(BluetoothAdapter myBluetoothAdapter,
String filter) {
Log.d(TAG, "(*) Initialising Bluetooth connection for device: " + filter);
if(myBluetoothAdapter.isEnabled()) {
for (BluetoothDevice pairedDevice : myBluetoothAdapter.getBondedDevices()) {
if (pairedDevice.getName().contains(filter /*Like MI*/)) {
Log.d(TAG, "\tDevice Name: " + pairedDevice.getName());
Log.d(TAG, "\tDevice MAC: " + pairedDevice.getAddress());
activeDevice = pairedDevice;
break;
}
}
}
Log.d(TAG, "\tDidnt find any device!");
}
示例2: onReceive
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@Override
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);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
示例3: connect
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@ReactMethod
public void connect(String peripheralUUID, Callback callback) {
Log.d(LOG_TAG, "Connect to: " + peripheralUUID );
synchronized(peripherals) {
Peripheral peripheral = peripherals.get(peripheralUUID);
Log.e(LOG_TAG, "peripheral " + peripheral);
if (peripheral == null) {
if (peripheralUUID != null) {
peripheralUUID = peripheralUUID.toUpperCase();
}
if (BluetoothAdapter.checkBluetoothAddress(peripheralUUID)) {
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(peripheralUUID);
peripheral = new Peripheral(device, reactContext);
peripherals.put(peripheralUUID, peripheral);
} else {
callback.invoke("Invalid peripheral uuid");
return;
}
}
peripheral.connect(callback, reactContext!=null?getCurrentActivity():context);
}
}
示例4: onReceive
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@Override
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);
// If it's already paired, skip it, because it's been listed already
if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
// When discovery is finished, change the Activity title
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
setProgressBarIndeterminateVisibility(false);
setTitle(R.string.select_device);
if (mNewDevicesArrayAdapter.getCount() == 0) {
String noDevices = getResources().getText(R.string.none_found).toString();
mNewDevicesArrayAdapter.add(noDevices);
}
}
}
示例5: onReceive
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@Override
public void onReceive(Context ctx, Intent intent) {
String action = intent.getAction();
if (action.equals(DISCOVERY_FINISHED)) {
LOG.info("Discovery finished");
ctx.unregisterReceiver(this);
finished.countDown();
} else if (action.equals(FOUND)) {
BluetoothDevice d = intent.getParcelableExtra(EXTRA_DEVICE);
if (LOG.isLoggable(INFO)) {
LOG.info("Discovered device: " +
scrubMacAddress(d.getAddress()));
}
addresses.add(d.getAddress());
}
}
示例6: getBondedDevices
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@ProtoMethod(description = "Send bluetooth serial message", example = "")
@ProtoMethodParam(params = {"string"})
public NativeArray getBondedDevices() {
start();
Set<BluetoothDevice> listDevices = mAdapter.getBondedDevices();
MLog.d(TAG, "listDevices " + listDevices);
int listSize = listDevices.size();
ProtocoderNativeArray array = new ProtocoderNativeArray(listSize);
MLog.d(TAG, "array " + array);
int counter = 0;
for (BluetoothDevice b : listDevices) {
MLog.d(TAG, "bt " + b);
ReturnObject btDevice = new ReturnObject();
btDevice.put("name", b.getName());
btDevice.put("mac", b.getAddress());
array.addPE(counter++, btDevice);
}
return array;
}
示例7: ConnectThread
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
} else {
tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
}
} catch (IOException | NullPointerException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
mState = STATE_CONNECTING;
}
示例8: isBluetoothHeadsetAvailable
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
public boolean isBluetoothHeadsetAvailable() {
ensureInit();
if (mBluetoothAdapter != null && mBluetoothAdapter.isEnabled() && mAudioManager != null && mAudioManager.isBluetoothScoAvailableOffCall()) {
boolean isHeadsetConnected = false;
if (mBluetoothHeadset != null) {
List<BluetoothDevice> devices = mBluetoothHeadset.getConnectedDevices();
mBluetoothDevice = null;
for (final BluetoothDevice dev : devices) {
if (mBluetoothHeadset.getConnectionState(dev) == BluetoothHeadset.STATE_CONNECTED) {
mBluetoothDevice = dev;
isHeadsetConnected = true;
break;
}
}
Log.d(isHeadsetConnected ? "[Bluetooth] Headset found, bluetooth audio route available" : "[Bluetooth] No headset found, bluetooth audio route unavailable");
}
return isHeadsetConnected;
}
return false;
}
示例9: createBondApi18
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
private boolean createBondApi18(final BluetoothDevice device) {
/*
* There is a createBond() method in BluetoothDevice class but for now it's hidden. We will call it using reflections. It has been revealed in KitKat (Api19)
*/
try {
final Method createBond = device.getClass().getMethod("createBond");
if (createBond != null) {
sendLogBroadcast(LOG_LEVEL_DEBUG, "gatt.getDevice().createBond() (hidden)");
return (Boolean) createBond.invoke(device);
}
} catch (final Exception e) {
Log.w(TAG, "An exception occurred while creating bond", e);
Log.e(TAG, e.toString());
}
return false;
}
示例10: unpairBondedDevices
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
private void unpairBondedDevices() {
Log.d(TAG, "unpairBondedDevices");
if (mBluetoothAdapter != null) {
Set<BluetoothDevice> bondedDevices = mBluetoothAdapter.getBondedDevices();
for (BluetoothDevice device : bondedDevices) {
Log.d(TAG, "bonded device:" + device.getName());
if (TARGET_DEVICE_NAME.contains(device.getName())) {
unpairDevice(device);
} else {
Log.d(TAG, "it is not target device");
}
}
} else {
Log.d(TAG, "mBluetoothAdapter!=null");
}
}
示例11: connect
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
/**
* Start the ConnectThread to initiate a connection to a remote device.
*
* @param device The BluetoothDevice to connect
* @param secure Socket Security type - Secure (true) , Insecure (false)
*/
public synchronized void connect(BluetoothDevice device, boolean secure) {
Log.d(TAG, "connect to: " + device);
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device, secure);
mConnectThread.start();
// Update UI title
updateUserInterfaceTitle();
}
示例12: ConnectBluetoothThread
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
public ConnectBluetoothThread(BluetoothDevice device, BluetoothAdapter mBluetoothAdapter, Handler handler) {
// Use a temporary object that is later assigned to mmSocket,
// because mmSocket is final
this.mBluetoothAdapter=mBluetoothAdapter;
this.handler=handler;
mmDevice = device;
BluetoothSocket tmp = null;
msg=new Message();
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
// MY_UUID is the app's UUID string, also used by the server code
String uuid="00001101-0000-1000-8000-00805f9b34fb";
tmp = device.createRfcommSocketToServiceRecord(UUID.fromString(uuid));
} catch (IOException e) {
Log.d(TAG, "ConnectBluetoothThread: 初始化uuid错误");
}
mmSocket = tmp;
}
示例13: onListItemClick
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = devicesListAdapter.getDevice(position);
if (device == null) return;
if (scanningBleDevices) {
bluetoothAdapter.stopLeScan(mLeScanCallback);
scanningBleDevices = false;
}
final Intent intent = new Intent();
intent.putExtra(EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(EXTRAS_DEVICE_ADDRESS, device.getAddress());
setResult(Activity.RESULT_OK, intent);
finish();
}
示例14: onListItemClick
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
@Override
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@SuppressWarnings("deprecation")
protected void onListItemClick(ListView l, View v, int position, long id) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
final Intent intent = new Intent(this, DeviceControlActivity.class);
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_NAME, device.getName());
intent.putExtra(DeviceControlActivity.EXTRAS_DEVICE_ADDRESS, device.getAddress());
if (mScanning) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBluetoothLeScanner.stopScan(new ScanCallback() {});
} else {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
mScanning = false;
}
startActivity(intent);
}
示例15: ConnectedThread
import android.bluetooth.BluetoothDevice; //导入依赖的package包/类
public ConnectedThread(BluetoothSocket socket, BluetoothDevice device) {
MLog.d(TAG, "create ConnectedThread");
mmSocket = socket;
mmDevice = device;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}