11package com .samsung .microbit .core .bluetooth ;
22
3+ import android .Manifest ;
4+ import android .annotation .SuppressLint ;
35import android .bluetooth .BluetoothAdapter ;
46import android .bluetooth .BluetoothDevice ;
57import android .bluetooth .BluetoothGattCharacteristic ;
68import android .bluetooth .BluetoothManager ;
79import android .content .Context ;
810import android .content .SharedPreferences ;
11+ import android .os .Build ;
912import android .provider .Settings ;
1013import android .util .Log ;
1114
1720
1821import static com .samsung .microbit .BuildConfig .DEBUG ;
1922
23+ import androidx .core .content .ContextCompat ;
24+ import androidx .core .content .PermissionChecker ;
25+
2026public class BluetoothUtils {
2127 private static final String TAG = BluetoothUtils .class .getSimpleName ();
2228
2329 public static final String PREFERENCES_KEY = "Microbit_PairedDevices" ;
2430 public static final String PREFERENCES_PAIREDDEV_KEY = "PairedDeviceDevice" ;
2531
32+ // sConnectedDevice used only as a buffer - actual value stored in prefs
2633 private static ConnectedDevice sConnectedDevice = new ConnectedDevice ();
2734
2835 private static void logi (String message ) {
@@ -37,19 +44,44 @@ public static SharedPreferences getPreferences(Context ctx) {
3744 return ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY , Context .MODE_MULTI_PROCESS );
3845 }
3946
40- public static int getTotalPairedMicroBitsFromSystem () {
41- int totalPairedMicroBits = 0 ;
42- BluetoothAdapter mBluetoothAdapter = ((BluetoothManager ) MBApp .getApp ().getSystemService (Context
43- .BLUETOOTH_SERVICE )).getAdapter ();
44- if (mBluetoothAdapter != null && !mBluetoothAdapter .isEnabled ()) {
45- Set <BluetoothDevice > pairedDevices = mBluetoothAdapter .getBondedDevices ();
46- for (BluetoothDevice bt : pairedDevices ) {
47- if (bt .getName ().contains ("micro:bit" )) {
48- ++totalPairedMicroBits ;
49- }
50- }
47+ public static ConnectedDevice deviceFromPrefs (Context ctx ) {
48+ SharedPreferences prefs = getPreferences ( ctx );
49+
50+ ConnectedDevice fromPrefs = null ;
51+
52+ if ( prefs .contains (PREFERENCES_PAIREDDEV_KEY )) {
53+ String pairedDeviceString = prefs .getString (PREFERENCES_PAIREDDEV_KEY , null );
54+ Gson gson = new Gson ();
55+ fromPrefs = gson .fromJson (pairedDeviceString , ConnectedDevice .class );
5156 }
52- return totalPairedMicroBits ;
57+ return fromPrefs ;
58+ }
59+
60+ public static void deviceToPrefs (Context ctx , ConnectedDevice toPrefs ) {
61+ SharedPreferences prefs = ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
62+ Context .MODE_MULTI_PROCESS );
63+ SharedPreferences .Editor editor = prefs .edit ();
64+ if ( toPrefs == null ) {
65+ editor .clear ();
66+ } else {
67+ Gson gson = new Gson ();
68+ String jsonActiveDevice = gson .toJson ( toPrefs );
69+ editor .putString (PREFERENCES_PAIREDDEV_KEY , jsonActiveDevice );
70+ }
71+ editor .apply ();
72+ }
73+
74+ private static boolean havePermission ( Context ctx , String permission ) {
75+ return ContextCompat .checkSelfPermission ( ctx , permission ) == PermissionChecker .PERMISSION_GRANTED ;
76+ }
77+
78+ private static boolean havePermissionsFlashing ( Context ctx ) {
79+ boolean yes = true ;
80+ if ( Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
81+ if ( !havePermission ( ctx , Manifest .permission .BLUETOOTH_CONNECT ))
82+ yes = false ;
83+ }
84+ return yes ;
5385 }
5486
5587 public static String parse (final BluetoothGattCharacteristic characteristic ) {
@@ -89,51 +121,23 @@ public static boolean inZenMode(Context paramContext) {
89121 }
90122
91123 public static void updateFirmwareMicrobit (Context ctx , String firmware ) {
92- SharedPreferences pairedDevicePref = ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
93- Context .MODE_MULTI_PROCESS );
94- if (pairedDevicePref .contains (PREFERENCES_PAIREDDEV_KEY )) {
95- String pairedDeviceString = pairedDevicePref .getString (PREFERENCES_PAIREDDEV_KEY , null );
96- Log .v ("BluetoothUtils" , "Updating the microbit firmware" );
97- ConnectedDevice deviceInSharedPref = new Gson ().fromJson (pairedDeviceString , ConnectedDevice .class );
98- deviceInSharedPref .mfirmware_version = firmware ;
99- setPairedMicroBit (ctx , deviceInSharedPref );
124+ ConnectedDevice fromPrefs = deviceFromPrefs (ctx );
125+ if ( fromPrefs != null ) {
126+ Log .v ("BluetoothUtils" , "Updating the microbit firmware version" );
127+ fromPrefs .mfirmware_version = firmware ;
128+ deviceToPrefs (ctx , fromPrefs );
100129 }
101130 }
102131
103132 public static void updateConnectionStartTime (Context ctx , long time ) {
104- SharedPreferences pairedDevicePref = ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
105- Context .MODE_MULTI_PROCESS );
106- if (pairedDevicePref .contains (PREFERENCES_PAIREDDEV_KEY )) {
107- String pairedDeviceString = pairedDevicePref .getString (PREFERENCES_PAIREDDEV_KEY , null );
108- Log .e ("BluetoothUtils" , "Updating the microbit firmware" );
109- ConnectedDevice deviceInSharedPref = new Gson ().fromJson (pairedDeviceString , ConnectedDevice .class );
110- deviceInSharedPref .mlast_connection_time = time ;
111- setPairedMicroBit (ctx , deviceInSharedPref );
133+ ConnectedDevice fromPrefs = deviceFromPrefs (ctx );
134+ if ( fromPrefs != null ) {
135+ Log .e ("BluetoothUtils" , "Updating the microbit connection time" );
136+ fromPrefs .mlast_connection_time = time ;
137+ deviceToPrefs (ctx , fromPrefs );
112138 }
113139 }
114140
115- public static BluetoothDevice getPairedDeviceMicroBit (Context context ) {
116- SharedPreferences pairedDevicePref = context .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
117- Context .MODE_MULTI_PROCESS );
118- if (pairedDevicePref .contains (PREFERENCES_PAIREDDEV_KEY )) {
119- String pairedDeviceString = pairedDevicePref .getString (PREFERENCES_PAIREDDEV_KEY , null );
120- Gson gson = new Gson ();
121- sConnectedDevice = gson .fromJson (pairedDeviceString , ConnectedDevice .class );
122- //Check if the microbit is still paired with our mobile
123- BluetoothAdapter mBluetoothAdapter = ((BluetoothManager ) MBApp .getApp ().getSystemService (Context
124- .BLUETOOTH_SERVICE )).getAdapter ();
125- if (mBluetoothAdapter .isEnabled ()) {
126- Set <BluetoothDevice > pairedDevices = mBluetoothAdapter .getBondedDevices ();
127- for (BluetoothDevice bt : pairedDevices ) {
128- if (bt .getAddress ().equals (sConnectedDevice .mAddress )) {
129- return bt ;
130- }
131- }
132- }
133- }
134- return null ;
135- }
136-
137141 public static ConnectedDevice getPairedMicrobit (Context ctx ) {
138142 SharedPreferences pairedDevicePref = ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
139143 Context .MODE_MULTI_PROCESS );
@@ -142,16 +146,18 @@ public static ConnectedDevice getPairedMicrobit(Context ctx) {
142146 sConnectedDevice = new ConnectedDevice ();
143147 }
144148
145- if (pairedDevicePref .contains (PREFERENCES_PAIREDDEV_KEY )) {
149+ ConnectedDevice fromPrefs = deviceFromPrefs (ctx );
150+ if ( fromPrefs == null ) {
151+ sConnectedDevice .mPattern = null ;
152+ sConnectedDevice .mName = null ;
153+ } else {
146154 boolean pairedMicrobitInSystemList = false ;
147- String pairedDeviceString = pairedDevicePref .getString (PREFERENCES_PAIREDDEV_KEY , null );
148- Gson gson = new Gson ();
149- sConnectedDevice = gson .fromJson (pairedDeviceString , ConnectedDevice .class );
155+ sConnectedDevice = fromPrefs ;
150156 //Check if the microbit is still paired with our mobile
151157 BluetoothAdapter mBluetoothAdapter = ((BluetoothManager ) MBApp .getApp ().getSystemService (Context
152158 .BLUETOOTH_SERVICE )).getAdapter ();
153- if (mBluetoothAdapter .isEnabled ()) {
154- Set <BluetoothDevice > pairedDevices = mBluetoothAdapter .getBondedDevices ();
159+ if (mBluetoothAdapter .isEnabled () && havePermissionsFlashing ( ctx ) ) {
160+ @ SuppressLint ( "MissingPermission" ) Set <BluetoothDevice > pairedDevices = mBluetoothAdapter .getBondedDevices ();
155161 for (BluetoothDevice bt : pairedDevices ) {
156162 if (bt .getAddress ().equals (sConnectedDevice .mAddress )) {
157163 pairedMicrobitInSystemList = true ;
@@ -176,26 +182,12 @@ public static ConnectedDevice getPairedMicrobit(Context ctx) {
176182
177183 setPairedMicroBit (ctx , null );
178184 }
179- } else {
180- sConnectedDevice .mPattern = null ;
181- sConnectedDevice .mName = null ;
182185 }
183186 return sConnectedDevice ;
184187 }
185188
186189 public static void setPairedMicroBit (Context ctx , ConnectedDevice newDevice ) {
187- SharedPreferences pairedDevicePref = ctx .getApplicationContext ().getSharedPreferences (PREFERENCES_KEY ,
188- Context .MODE_MULTI_PROCESS );
189- SharedPreferences .Editor editor = pairedDevicePref .edit ();
190- if (newDevice == null ) {
191- editor .clear ();
192- } else {
193- Gson gson = new Gson ();
194- String jsonActiveDevice = gson .toJson (newDevice );
195- editor .putString (PREFERENCES_PAIREDDEV_KEY , jsonActiveDevice );
196- }
197-
198- editor .apply ();
190+ deviceToPrefs ( ctx , newDevice );
199191 }
200192
201193}
0 commit comments