本文整理汇总了Java中android.bluetooth.BluetoothDevice.ACTION_FOUND属性的典型用法代码示例。如果您正苦于以下问题:Java BluetoothDevice.ACTION_FOUND属性的具体用法?Java BluetoothDevice.ACTION_FOUND怎么用?Java BluetoothDevice.ACTION_FOUND使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类android.bluetooth.BluetoothDevice
的用法示例。
在下文中一共展示了BluetoothDevice.ACTION_FOUND属性的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onReceive
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothDevice.ACTION_FOUND:
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "ACTION_FOUND: " + device.getName());
if (device.getName() != null && TARGET_DEVICE_NAME.contains(device.getName())) {
Log.d(TAG, "target device found..");
mTargetDevices.add(device);
}
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
int previousState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1);
Log.d(TAG, "ACTION_BOND_STATE_CHANGED: state:" + state + ", previous:" + previousState);
break;
}
}
示例2: startBluetoothServices
/**
* Starts bluetooth and listens to pairing requests and bluetooth state changes
*/
public static void startBluetoothServices() {
mReceiver = new BluetoothReceiver();
//Register the BroadcastReceiver for multiple bluetooth actions
//when another bluetooth device is discovered in range
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
//when discovering action finishes (discovering means that bluetooth searches for nearby devices)
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//when bluetooth changes from on to off
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
//when another device requests pairing with this device
filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
mainActivity.mainRegisterReceiver(mReceiver, filter);
if(!started) {
//tries to start bluetooth if it is not on
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter!=null){
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
mainActivity.startActivityForResult(enableBtIntent, MainActivity.BLUETOOTH_ON);
} else {
started = true;
}
}
}
}
示例3: onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bluetooth);
listView = (ListView) findViewById(R.id.listview);
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1);
listView.setAdapter(mArrayAdapter);
//1.获取BluetoothAdapter;
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
//2.启用蓝牙
if (!bluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
} else {
//查询配对信息
findDevice();
discoverable();
}
} else {
// 不支持蓝牙
Toast.makeText(this, "不支持蓝牙设备", Toast.LENGTH_LONG).show();
}
}
示例4: scanNetworks
@ProtoMethod(description = "Scan bluetooth networks. Gives back the name, mac and signal strength", example = "")
@ProtoMethodParam(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("strength", rssi);
callbackfn.event(o);
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
getContext().registerReceiver(mReceiver, filter);
}
示例5: onCreateView
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=LayoutInflater.from(getActivity()).inflate(R.layout.fragment_linkdevice,container,false);
//mBtn_searchBle= (Button) view.findViewById(R.id.btn_search);
mCpb_connect= (CircularProgressButton) view.findViewById(R.id.cpb_connect);
iv_connect= (ImageView) view.findViewById(R.id.iv_conntect);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, mArrayList);
locationUtils=new LocationUtils(getApplicationContext());
vibrator = (Vibrator) getActivity(). getSystemService(VIBRATOR_SERVICE);
mApplication= (MyApplication) getActivity().getApplication();
mCpb_connect.setIndeterminateProgressMode(true);
mCpb_connect.setOnClickListener(new mCpb_connectClick());
//打开蓝牙
openBtDevice();
// 动态注册广播接收器
// 用来接收扫描到的设备信息
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
getActivity().registerReceiver(mReceiver, filter);
return view;
}
示例6: onReceive
/**
* {@inheritDoc}
*/
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "Incoming intent : " + action);
switch (action) {
case BluetoothDevice.ACTION_FOUND :
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, "Device discovered! " + BluetoothController.deviceToString(device));
listener.onDeviceDiscovered(device);
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED :
// Discovery has ended.
Log.d(TAG, "Discovery ended.");
listener.onDeviceDiscoveryEnd();
break;
case BluetoothAdapter.ACTION_STATE_CHANGED :
// Discovery state changed.
Log.d(TAG, "Bluetooth state changed.");
listener.onBluetoothStatusChanged();
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED :
// Pairing state has changed.
Log.d(TAG, "Bluetooth bonding state changed.");
listener.onDevicePairingEnded();
break;
default :
// Does nothing.
break;
}
}
示例7: onReceive
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch (action) {
case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
mDeviceList = new ArrayList<>();
showProgress("", mContext.getString(R.string.searching_bluetooth_devices));
break;
case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
performDiscoveryFinishedAction();
break;
case BluetoothDevice.ACTION_FOUND:
performActionFound(intent);
break;
case BluetoothDevice.ACTION_BOND_STATE_CHANGED: {
performBondStateChangeAction(intent);
break;
}
default:
break;
}
}
示例8: scanAroundDevices
private void scanAroundDevices()
{
displayLog("Intentando escanerar dispositivos...");
if (mReceiver == null)
{
// Register the BroadcastReceiver
mReceiver = new DeviceBroadcastReceiver();
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
// Start scanning
mBluetoothAdapter.startDiscovery();
}
示例9: find
@Override
public void find() {
AfterEnable afterEnableFind = new AfterEnable() {
@Override
public void after() {
find();
}
};
if (!enable(afterEnableFind)) {
return;
}
if (findInProgress) {
return;
}
onStatusChange(BluetoothStatus.Searching);
stopFindTask = new TimerTask() {
@Override
public void run() {
cancelFind();
stopFindTask = null;
}
};
// stop find after 2 minutes.
stopFindTimer = new Timer();
stopFindTimer.schedule(stopFindTask, 120 * 1000);
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
app.registerReceiver(mReceiver, filter);
findInProgress = true;
mBt.startDiscovery();
}
示例10: btDiscoverAndGetResults
@Rpc(
description =
"Start discovery, wait for discovery to complete, and return results, which is a list of "
+ "serialized BluetoothDevice objects."
)
public List<Bundle> btDiscoverAndGetResults()
throws InterruptedException, BluetoothAdapterSnippetException {
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mDiscoveryResults.clear();
mIsScanResultAvailable = false;
BroadcastReceiver receiver = new BluetoothScanReceiver();
mContext.registerReceiver(receiver, filter);
try {
if (!mBluetoothAdapter.startDiscovery()) {
throw new BluetoothAdapterSnippetException(
"Failed to initiate Bluetooth Discovery.");
}
if (!Utils.waitUntil(() -> mIsScanResultAvailable, 120)) {
throw new BluetoothAdapterSnippetException(
"Failed to get discovery results after 2 mins, timeout!");
}
} finally {
mContext.unregisterReceiver(receiver);
}
return btGetCachedScanResults();
}
示例11: onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.device_finder_layout);
android.widget.Button btnConnect = (android.widget.Button) findViewById(R.id.btn_connect);
TextView tvDevice = (TextView) findViewById(R.id.tv_device);
btnConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!deviceAddress.isEmpty())
connect();
}
});
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Log.w(TAG, "No default Bluetooth adapter. Device likely does not support bluetooth.");
return;
}
if(!mBluetoothAdapter.isEnabled()){
Log.w(TAG, "Enable BT");
mBluetoothAdapter.enable();
}
Set<BluetoothDevice> allDevices = mBluetoothAdapter.getBondedDevices();
for(BluetoothDevice d: allDevices){
if(isNanoHubDevice(d)){
myDevices.add(d);
}
}
Log.w(TAG, "myDevices size = " + myDevices.size());
if(myDevices.size() == 0){
Toast.makeText(this, "No device found", Toast.LENGTH_LONG).show();
// If no device was already paired let's try to discover
Log.w(TAG, "No device found");
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
myDevices.clear();
Boolean stat = mBluetoothAdapter.startDiscovery();
if(stat)
Log.w(TAG, "discovering started");
else
Log.w(TAG, "Could not start discovery");
}else {
tvDevice.setText("Device: " + myDevices.get(0).getName() + "\t" + myDevices.get(0).getAddress());
deviceAddress = myDevices.get(0).getAddress();
}
if(mBluetoothAdapter.isDiscovering()){
mBluetoothAdapter.cancelDiscovery();
}
}