Java Code Examples for android.bluetooth.BluetoothDevice#getType()

The following examples show how to use android.bluetooth.BluetoothDevice#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractScanner.java    From easyble-x with Apache License 2.0 6 votes vote down vote up
void parseScanResult(BluetoothDevice device, boolean isConnectedBySys, @Nullable ScanResult result, int rssi, byte[] scanRecord) {
    if ((configuration.onlyAcceptBleDevice && device.getType() != BluetoothDevice.DEVICE_TYPE_LE) ||
            !device.getAddress().matches("^[0-9A-F]{2}(:[0-9A-F]{2}){5}$")) {
        return;
    }
    String name = device.getName() == null ? "" : device.getName();
    if (configuration.rssiLowLimit <= rssi) {
        //通过构建器实例化Device
        Device dev = deviceCreator.create(device, result);
        if (dev != null) {
            dev.name = TextUtils.isEmpty(dev.getName()) ? name : dev.getName();
            dev.rssi = rssi;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                dev.scanResult = result;
            }
            dev.scanRecord = scanRecord;
            handleScanCallback(false, dev, isConnectedBySys, -1, "");
        }
    }
    String msg = String.format(Locale.US, "found device! [name: %s, addr: %s]", TextUtils.isEmpty(name) ? "N/A" : name, device.getAddress());
    logger.log(Log.DEBUG, Logger.TYPE_SCAN_STATE, msg);
}
 
Example 2
Source File: BleManager.java    From Bluefruit_LE_Connect_Android_V2 with MIT License 6 votes vote down vote up
public @NonNull
List<BluetoothDevice> getConnectedDevices() {
    List<BluetoothDevice> connectedDevices = new ArrayList<>();

    // Check if already initialized
    if (mManager == null) {
        return connectedDevices;
    }

    List<BluetoothDevice> devices = mManager.getConnectedDevices(BluetoothProfile.GATT);
    for (BluetoothDevice device : devices) {
        final int type = device.getType();
        if (type == BluetoothDevice.DEVICE_TYPE_LE || type == BluetoothDevice.DEVICE_TYPE_DUAL) {
            connectedDevices.add(device);
        }
    }

    return connectedDevices;
}
 
Example 3
Source File: ListActivity.java    From myo_AndoridEMG with MIT License 6 votes vote down vote up
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
// Device Log
    ParcelUuid[] uuids = device.getUuids();
    String uuid = "";
    if (uuids != null) {
        for (ParcelUuid puuid : uuids) {
            uuid += puuid.toString() + " ";
        }
    }

    String msg = "name=" + device.getName() + ", bondStatus="
            + device.getBondState() + ", address="
            + device.getAddress() + ", type" + device.getType()
            + ", uuids=" + uuid;
    Log.d("BLEActivity", msg);

    if (device.getName() != null && !deviceNames.contains(device.getName())) {
        deviceNames.add(device.getName());
    }
}
 
Example 4
Source File: DevicesFragment.java    From SimpleBluetoothTerminal with MIT License 5 votes vote down vote up
void refresh() {
    listItems.clear();
    if(bluetoothAdapter != null) {
        for (BluetoothDevice device : bluetoothAdapter.getBondedDevices())
            if (device.getType() != BluetoothDevice.DEVICE_TYPE_LE)
                listItems.add(device);
    }
    Collections.sort(listItems, DevicesFragment::compareTo);
    listAdapter.notifyDataSetChanged();
}
 
Example 5
Source File: BLEPeripheralDefault.java    From itag with GNU General Public License v3.0 5 votes vote down vote up
BLEPeripheralDefault(@NonNull Context context, @NonNull BLECentralManagerInterface manager, @NonNull BluetoothDevice device) {
    this.device = device;
    this.manager = manager;
    this.context = context;
    int type = device.getType();
    if (BuildConfig.DEBUG) {
        Log.d(LT, "BLEPeripheralDefault addr=" + device.getAddress() + " name=" + device.getName() + " type=" + type);
    }
    this.cached = type != DEVICE_TYPE_UNKNOWN;
}
 
Example 6
Source File: BluetoothMainActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
@Override
protected void init() {
    tv_detail = (TextView) findViewById(R.id.tv_detail);
    bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null) {
        Toast.makeText(this, "Your device do not support Bluetooth.", Toast.LENGTH_SHORT).show();
        return;
    }
    if (!bluetoothAdapter.isEnabled()) {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(intent, RequestBluetoothCode);
    } else {
        Set<BluetoothDevice> set = bluetoothAdapter.getBondedDevices();
        for (BluetoothDevice device : set) {
            bondedDevices.add(device);
            String text = device.getName() + " address: " + device.getAddress() + " type: " + device.getType() + "\n";
            tv_detail.append(text);
        }
    }

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
    registerReceiver(bluetoothReceiver, intentFilter);

    bluetoothChatService = new BluetoothChatService(this, handler);
    bluetoothChatService.start();
}
 
Example 7
Source File: BluetoothMainActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
private void loadDiscoverDevices() {
    tv_detail.append("发现设备:\n");
    if (foundDevices.size() > 0) {
        for (BluetoothDevice device : foundDevices) {
            String text = device.getName() + " address: " + device.getAddress() + " type: " + device.getType() + "\n";
            tv_detail.append(text);
        }
    }
}
 
Example 8
Source File: BluetoothMainActivity.java    From AndroidDemo with MIT License 5 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RequestBluetoothCode) {
        if (resultCode == RESULT_OK) {
            Set<BluetoothDevice> set = bluetoothAdapter.getBondedDevices();
            bondedDevices.clear();
            for (BluetoothDevice device : set) {
                bondedDevices.add(device);
                String text = device.getName() + " address: " + device.getAddress() + " type: " + device.getType() + "\n";
                tv_detail.append(text);
            }
        } else
            Toast.makeText(this, "Open Bluetooth is failed,please open in Setting.", Toast.LENGTH_SHORT).show();
    } else
        super.onActivityResult(requestCode, resultCode, data);
}
 
Example 9
Source File: BluetoothDeviceInfo.java    From libcommon with Apache License 2.0 5 votes vote down vote up
BluetoothDeviceInfo(final BluetoothDevice device) {
	name = device.getName();
	address =  device.getAddress();
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
		type = device.getType();
	} else {
		type = 0;
	}
	final BluetoothClass clazz = device.getBluetoothClass();
	deviceClass = clazz != null ? clazz.getDeviceClass() : 0;
	bondState = device.getBondState();
}
 
Example 10
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
private void findNearbyDevicesBackgroundScan(DumperContext dumpContext) {
    int n = 180;
    StringBuilder builder = new StringBuilder();
    String status = PASS_STATUS;
    String error = "";
    if (!isJsonFormat) {
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
        format(dumpContext, "| %1$32s | %2$32s | %3$32s | %4$32s | %5$32s\n",
            "Name",
            "Address",
            "BtName", "Origin", "RSSI");
    }
    JSONArray jsonArray = new JSONArray();
    Iterable<String> iterable = clientConnections.keySet();
    try {
        for (String mac : iterable) {
            BluetoothDevice device = fitbitGatt.getBluetoothDevice(mac);
            if (device == null) {
                continue;
            }
            String type;
            switch (device.getType()) {
                case DEVICE_TYPE_CLASSIC:
                    type = "classic";
                    break;
                case DEVICE_TYPE_DUAL:
                    type = "dual";
                    break;
                case DEVICE_TYPE_LE:
                    type = "low energy";
                    break;
                default:
                    type = "unknown";
            }
            String deviceName = new GattUtils().debugSafeGetBtDeviceName(device);
            String deviceAddress = device.getAddress();
            String origin = clientConnections.get(mac).getDevice().getOrigin().name();
            String rssi = String.valueOf(clientConnections.get(mac).getDevice().getRssi());
            Map<String, Object> map = new LinkedHashMap<String, Object>() {{
                put("name", deviceName);
                put("address", deviceAddress);
                put("btname", type);
                put("origin", origin);
                put("rssi", rssi);
            }};
            JSONObject jsonObject = makeJsonObject(map);
            jsonArray.put(jsonObject);
            if (!isJsonFormat) {
                format(dumpContext, "| %1$32s | %2$32s | %3$32s | %4$32s | %5$32s\n",
                    deviceName, deviceAddress, type, origin, rssi);
            }
        }
    } catch (Exception e) {
        status = FAIL_STATUS;
        error = Arrays.toString(e.getStackTrace());
    }

    if (!isJsonFormat) {
        builder = new StringBuilder();
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
    } else {
        logJsonResult(dumpContext, status, error, jsonArray);
    }
    // look for everything just for the sake of this call
    ScanFilter filter = new ScanFilter.Builder().build();
    ArrayList<ScanFilter> scanFilters = new ArrayList<>(1);
    scanFilters.add(filter);
    boolean didStart = fitbitGatt.startSystemManagedPendingIntentScan(fitbitGatt.getAppContext(), scanFilters);
    if(!didStart) {
        log(dumpContext, "Scanner couldn't be started");
    }
}
 
Example 11
Source File: GattPlugin.java    From bitgatt with Mozilla Public License 2.0 4 votes vote down vote up
private void findNearbyDevices(DumperContext dumpContext) {
    int n = 180;
    StringBuilder builder = new StringBuilder();
    String status = PASS_STATUS;
    String error = "";
    if (!isJsonFormat) {
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
        format(dumpContext, "| %1$32s | %2$32s | %3$32s | %4$32s | %5$32s\n",
            "Name",
            "Address",
            "BtName", "Origin", "RSSI");
    }
    JSONArray jsonArray = new JSONArray();
    Iterable<String> iterable = clientConnections.keySet();
    try {
        for (String mac : iterable) {
            BluetoothDevice device = fitbitGatt.getBluetoothDevice(mac);
            if (device == null) {
                continue;
            }
            String type;
            switch (device.getType()) {
                case DEVICE_TYPE_CLASSIC:
                    type = "classic";
                    break;
                case DEVICE_TYPE_DUAL:
                    type = "dual";
                    break;
                case DEVICE_TYPE_LE:
                    type = "low energy";
                    break;
                default:
                    type = "unknown";
            }
            String deviceName = new GattUtils().debugSafeGetBtDeviceName(device);
            String deviceAddress = device.getAddress();
            String origin = clientConnections.get(mac).getDevice().getOrigin().name();
            String rssi = String.valueOf(clientConnections.get(mac).getDevice().getRssi());
            Map<String, Object> map = new LinkedHashMap<String, Object>() {{
                put("name", deviceName);
                put("address", deviceAddress);
                put("btname", type);
                put("origin", origin);
                put("rssi", rssi);
            }};
            JSONObject jsonObject = makeJsonObject(map);
            jsonArray.put(jsonObject);
            if (!isJsonFormat) {
                format(dumpContext, "| %1$32s | %2$32s | %3$32s | %4$32s | %5$32s\n",
                    deviceName, deviceAddress, type, origin, rssi);
            }
        }
    } catch (Exception e) {
        status = FAIL_STATUS;
        error = Arrays.toString(e.getStackTrace());
    }

    if (!isJsonFormat) {
        builder = new StringBuilder();
        for (int i = 0; i < n; i++) {
            builder.append("=");
        }
        log(dumpContext, builder.toString());
    } else {
        logJsonResult(dumpContext, status, error, jsonArray);
    }
    fitbitGatt.startHighPriorityScan(fitbitGatt.getAppContext());
}
 
Example 12
Source File: RileyLinkBLEScanActivity.java    From AndroidAPS with GNU Affero General Public License v3.0 4 votes vote down vote up
private String getDeviceDebug(BluetoothDevice device) {
    return "BluetoothDevice [name=" + device.getName() + ", address=" + device.getAddress() + //
            ", type=" + device.getType(); // + ", alias=" + device.getAlias();
}
 
Example 13
Source File: BluetoothScanWorker.java    From PhoneProfilesPlus with Apache License 2.0 4 votes vote down vote up
static int getBluetoothType(BluetoothDevice device) {
    //if (android.os.Build.VERSION.SDK_INT >= 18)
    return device.getType();
    //else
    //    return 1; // BluetoothDevice.DEVICE_TYPE_CLASSIC
}