mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-24 17:23:03 +02:00
experimental Bluetooth search with new API.
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
|
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
|
||||||
|
|
||||||
|
@@ -17,9 +17,12 @@ package com.health.openscale.gui.preferences;
|
|||||||
|
|
||||||
import android.bluetooth.BluetoothAdapter;
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothDevice;
|
import android.bluetooth.BluetoothDevice;
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.os.Handler;
|
|
||||||
import android.preference.CheckBoxPreference;
|
import android.preference.CheckBoxPreference;
|
||||||
import android.preference.EditTextPreference;
|
import android.preference.EditTextPreference;
|
||||||
import android.preference.ListPreference;
|
import android.preference.ListPreference;
|
||||||
@@ -30,6 +33,7 @@ import android.preference.PreferenceGroup;
|
|||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.preference.PreferenceScreen;
|
import android.preference.PreferenceScreen;
|
||||||
import android.widget.BaseAdapter;
|
import android.widget.BaseAdapter;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.health.openscale.R;
|
import com.health.openscale.R;
|
||||||
import com.health.openscale.core.bluetooth.BluetoothCommunication;
|
import com.health.openscale.core.bluetooth.BluetoothCommunication;
|
||||||
@@ -50,66 +54,70 @@ public class BluetoothPreferences extends PreferenceFragment implements SharedPr
|
|||||||
private PreferenceScreen btScanner;
|
private PreferenceScreen btScanner;
|
||||||
|
|
||||||
private BluetoothAdapter btAdapter = null;
|
private BluetoothAdapter btAdapter = null;
|
||||||
private Handler searchHandler = null;
|
|
||||||
private BluetoothAdapter.LeScanCallback scanCallback = null;
|
|
||||||
|
|
||||||
private Map<String, String> foundDevices = new HashMap<>();
|
private Map<String, String> foundDevices = new HashMap<>();
|
||||||
|
|
||||||
public void startSearching() {
|
public void startSearching() {
|
||||||
foundDevices.clear();
|
foundDevices.clear();
|
||||||
|
|
||||||
if (scanCallback == null)
|
IntentFilter filter = new IntentFilter();
|
||||||
{
|
|
||||||
scanCallback = new BluetoothAdapter.LeScanCallback()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
|
|
||||||
if (device.getName() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
foundDevices.put(device.getAddress(), device.getName());
|
|
||||||
|
|
||||||
btScanner.removeAll();
|
filter.addAction(BluetoothDevice.ACTION_FOUND);
|
||||||
|
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
|
||||||
|
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
|
||||||
|
|
||||||
for (Map.Entry<String, String> entry : foundDevices.entrySet())
|
getActivity().registerReceiver(mReceiver, filter);
|
||||||
{
|
btAdapter.startDiscovery();
|
||||||
if (getActivity() != null) {
|
}
|
||||||
Preference prefBtDevice = new Preference(getActivity().getBaseContext());
|
|
||||||
prefBtDevice.setSummary(entry.getKey());
|
|
||||||
|
|
||||||
for (BluetoothCommunication.BT_DEVICE_ID btScaleID : BluetoothCommunication.BT_DEVICE_ID.values()) {
|
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
|
||||||
BluetoothCommunication btDevice = BluetoothCommunication.getBtDevice(getActivity().getBaseContext(), btScaleID);
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
String action = intent.getAction();
|
||||||
|
|
||||||
if (btDevice.checkDeviceName(entry.getValue())) {
|
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
|
||||||
prefBtDevice.setOnPreferenceClickListener(new onClickListenerDeviceSelect());
|
//discovery starts, we can show progress dialog or perform other tasks
|
||||||
prefBtDevice.setKey(entry.getKey());
|
Toast.makeText(getActivity().getApplicationContext(), R.string.label_bluetooth_searching, Toast.LENGTH_SHORT).show();
|
||||||
prefBtDevice.setIcon(R.drawable.ic_bluetooth_connection_lost);
|
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
|
||||||
prefBtDevice.setTitle(entry.getValue() + " [" + btDevice.deviceName() + "]");
|
//discovery finishes, dismis progress dialog
|
||||||
break;
|
Toast.makeText(getActivity().getApplicationContext(), R.string.label_bluetooth_searching_finished, Toast.LENGTH_SHORT).show();
|
||||||
} else {
|
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
||||||
prefBtDevice.setIcon(R.drawable.ic_bluetooth_disabled);
|
//bluetooth device found
|
||||||
prefBtDevice.setTitle(entry.getValue() + " [" + getResources().getString(R.string.label_bt_device_no_support) + "]");
|
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
||||||
}
|
|
||||||
|
foundDevices.put(device.getAddress(), device.getName());
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> entry : foundDevices.entrySet()) {
|
||||||
|
if (getActivity() != null) {
|
||||||
|
Preference prefBtDevice = new Preference(getActivity().getBaseContext());
|
||||||
|
prefBtDevice.setSummary(entry.getKey());
|
||||||
|
|
||||||
|
for (BluetoothCommunication.BT_DEVICE_ID btScaleID : BluetoothCommunication.BT_DEVICE_ID.values()) {
|
||||||
|
BluetoothCommunication btDevice = BluetoothCommunication.getBtDevice(getActivity().getBaseContext(), btScaleID);
|
||||||
|
|
||||||
|
if (btDevice.checkDeviceName(entry.getValue())) {
|
||||||
|
prefBtDevice.setOnPreferenceClickListener(new onClickListenerDeviceSelect());
|
||||||
|
prefBtDevice.setKey(entry.getKey());
|
||||||
|
prefBtDevice.setIcon(R.drawable.ic_bluetooth_connection_lost);
|
||||||
|
prefBtDevice.setTitle(entry.getValue() + " [" + btDevice.deviceName() + "]");
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
prefBtDevice.setIcon(R.drawable.ic_bluetooth_disabled);
|
||||||
|
prefBtDevice.setTitle(entry.getValue() + " [" + getResources().getString(R.string.label_bt_device_no_support) + "]");
|
||||||
}
|
}
|
||||||
|
|
||||||
btScanner.addPreference(prefBtDevice);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btScanner.addPreference(prefBtDevice);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
searchHandler.postDelayed(new Runnable()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
btAdapter.stopLeScan(scanCallback);
|
|
||||||
}
|
}
|
||||||
}, 10000);
|
}
|
||||||
|
};
|
||||||
|
|
||||||
btAdapter.startLeScan(scanCallback);
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
|
getActivity().unregisterReceiver(mReceiver);
|
||||||
|
|
||||||
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -117,7 +125,6 @@ public class BluetoothPreferences extends PreferenceFragment implements SharedPr
|
|||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
btAdapter = BluetoothAdapter.getDefaultAdapter();
|
||||||
searchHandler = new Handler();
|
|
||||||
|
|
||||||
addPreferencesFromResource(R.xml.bluetooth_preferences);
|
addPreferencesFromResource(R.xml.bluetooth_preferences);
|
||||||
|
|
||||||
|
@@ -127,6 +127,7 @@
|
|||||||
<string name="label_bluetooth_title">Bluetooth</string>
|
<string name="label_bluetooth_title">Bluetooth</string>
|
||||||
<string name="label_bluetooth_enable">Search for scale on startup</string>
|
<string name="label_bluetooth_enable">Search for scale on startup</string>
|
||||||
<string name="label_bluetooth_searching">Searching for Bluetooth scales</string>
|
<string name="label_bluetooth_searching">Searching for Bluetooth scales</string>
|
||||||
|
<string name="label_bluetooth_searching_finished">Finished searching for Bluetooth scales</string>
|
||||||
<string name="label_device_type">Device Type</string>
|
<string name="label_device_type">Device Type</string>
|
||||||
|
|
||||||
<string name="label_enable_labels">Label on data point</string>
|
<string name="label_enable_labels">Label on data point</string>
|
||||||
|
@@ -4,7 +4,7 @@
|
|||||||
android:key="btScanner"
|
android:key="btScanner"
|
||||||
android:title="@string/label_device_type"
|
android:title="@string/label_device_type"
|
||||||
android:persistent="false">
|
android:persistent="false">
|
||||||
<Preference android:key="0" android:title="@string/label_bluetooth_searching"/>
|
<Preference android:key="0" android:title="@string/label_device_type"/>
|
||||||
</PreferenceScreen>
|
</PreferenceScreen>
|
||||||
<CheckBoxPreference android:title="@string/label_bluetooth_enable" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="btEnable" android:defaultValue="false"/>
|
<CheckBoxPreference android:title="@string/label_bluetooth_enable" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="btEnable" android:defaultValue="false"/>
|
||||||
<CheckBoxPreference android:title="@string/label_smartUserAssign" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="smartUserAssign" android:defaultValue="false"/>
|
<CheckBoxPreference android:title="@string/label_smartUserAssign" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="smartUserAssign" android:defaultValue="false"/>
|
||||||
|
Reference in New Issue
Block a user