當前位置: 首頁>>代碼示例>>Java>>正文


Java BluetoothDevice類代碼示例

本文整理匯總了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!");
}
 
開發者ID:yonixw,項目名稱:mi-band-2,代碼行數:19,代碼來源:BLEMiBand2Helper.java

示例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);
        }
    }
}
 
開發者ID:haraldh,項目名稱:iconsole-android,代碼行數:23,代碼來源:DeviceListActivity.java

示例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);
	}
}
 
開發者ID:lenglengiOS,項目名稱:react-native-blue-manager,代碼行數:24,代碼來源:BleManager.java

示例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);
        }
    }
}
 
開發者ID:sdrausty,項目名稱:buildAPKsSamples,代碼行數:23,代碼來源:DeviceListActivity.java

示例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());
	}
}
 
開發者ID:rafjordao,項目名稱:Nird2,代碼行數:17,代碼來源:DroidtoothPlugin.java

示例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;
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:26,代碼來源:PBluetooth.java

示例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;
}
 
開發者ID:zeevy,項目名稱:grblcontroller,代碼行數:18,代碼來源:SerialThreadService.java

示例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;
}
 
開發者ID:treasure-lau,項目名稱:Linphone4Android,代碼行數:22,代碼來源:BluetoothManager.java

示例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;
  }
 
開發者ID:Samsung,項目名稱:microbit,代碼行數:18,代碼來源:DfuBaseService.java

示例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");
    }
}
 
開發者ID:casper-kim,項目名稱:androidthings-bluetooth-pairing,代碼行數:17,代碼來源:MainActivity.java

示例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();
}
 
開發者ID:alexmerz,項目名稱:dab-iot,代碼行數:30,代碼來源:BluetoothChatService.java

示例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;
}
 
開發者ID:Axe-Ishmael,項目名稱:Blockly,代碼行數:21,代碼來源:ConnectBluetoothThread.java

示例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();
}
 
開發者ID:masterjc,項目名稱:bluewatcher,代碼行數:17,代碼來源:WatchSelectorActivity.java

示例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);
}
 
開發者ID:igrow-systems,項目名稱:igrow-android,代碼行數:20,代碼來源:DeviceScanActivity.java

示例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;
}
 
開發者ID:victordiaz,項目名稱:phonk,代碼行數:19,代碼來源:PBluetoothClient.java


注:本文中的android.bluetooth.BluetoothDevice類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。