質問編集履歴

3

サブクラスの追加

2021/07/03 14:36

投稿

iruma
iruma

スコア11

test CHANGED
File without changes
test CHANGED
@@ -194,6 +194,48 @@
194
194
 
195
195
  ```
196
196
 
197
+ ```java
198
+
199
+ private MainActivity m = new MainActivity();
200
+
201
+
202
+
203
+ //ペアデバイスを取得してサーバーに接続する
204
+
205
+ public void scanAndConnect(Context context,BluetoothAdapter bluetoothAdapter){
206
+
207
+ pairedDevices = bluetoothAdapter.getBondedDevices();
208
+
209
+ if (pairedDevices.size() > 0) {
210
+
211
+ for (BluetoothDevice device : pairedDevices) {
212
+
213
+ Log.d("pairedDevice","Name:"+device.getName()+"Address:"+device.getAddress());
214
+
215
+ Toast.makeText(context,"接続を開始します",Toast.LENGTH_SHORT).show();
216
+
217
+ device = bluetoothAdapter.getRemoteDevice(device.getAddress());
218
+
219
+ bluetoothGatt = device.connectGatt(context, false, bluetoothGattCallback);
220
+
221
+ m.setTextName("" + device.getName());
222
+
223
+ }
224
+
225
+ }
226
+
227
+ if (bluetoothGatt == null) {
228
+
229
+ Toast.makeText(context, "ペアリングされたデバイスが存在しません", Toast.LENGTH_SHORT).show();
230
+
231
+ }
232
+
233
+ Toast.makeText(context,"Bluetooth通信に異常が発生しています",Toast.LENGTH_LONG).show();
234
+
235
+ }
236
+
237
+ ```
238
+
197
239
 
198
240
 
199
241
  ### 試したこと

2

エラーメッセージを変更しそびれていたので変更しました

2021/07/03 14:35

投稿

iruma
iruma

スコア11

test CHANGED
File without changes
test CHANGED
@@ -22,29 +22,15 @@
22
22
 
23
23
  E/AndroidRuntime: FATAL EXCEPTION: main
24
24
 
25
- Process: com.example.esp32ble, PID: 26798
25
+ Process: com.example.esp32ble, PID: 29278
26
-
26
+
27
- java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
27
+ java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
28
-
29
- at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:163)
28
+
30
-
31
- at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
32
-
33
- at android.content.Context.obtainStyledAttributes(Context.java:738)
34
-
35
- at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:839)
36
-
37
- at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:806)
38
-
39
- at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:630)
40
-
41
- at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:223)
42
-
43
- at com.example.esp32ble.MainActivity.setTextName(MainActivity.java:167)
29
+ at com.example.esp32ble.MainActivity.setTextName(MainActivity.java:174)
44
30
 
45
31
  at com.example.esp32ble.SubsidyBle.scanAndConnect(SubsidyBle.java:121)
46
32
 
47
- at com.example.esp32ble.MainActivity.chartChange(MainActivity.java:152)
33
+ at com.example.esp32ble.MainActivity.chartChange(MainActivity.java:160)
48
34
 
49
35
  at com.example.esp32ble.-$$Lambda$TW4Z-_TbHlSaQ3_9pMVQ_fCEm0w.onClick(Unknown Source:2)
50
36
 
@@ -70,6 +56,8 @@
70
56
 
71
57
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
72
58
 
59
+
60
+
73
61
  ```
74
62
 
75
63
 

1

onCreateにfindViewByIdの移動をしTextViewのnameTextをグローバル変数にした

2021/07/03 14:12

投稿

iruma
iruma

スコア11

test CHANGED
File without changes
test CHANGED
@@ -92,6 +92,8 @@
92
92
 
93
93
  private Button changeButton;
94
94
 
95
+ private TextView nameText;
96
+
95
97
  private final String NOW_CONNECT = "接続を終了";
96
98
 
97
99
  private final String NOW_DISCONNECT = "接続を開始";
@@ -134,7 +136,9 @@
134
136
 
135
137
  changeButton.setOnClickListener(this::chartChange);
136
138
 
137
-
139
+ nameText = findViewById(R.id.device_name_text);
140
+
141
+
138
142
 
139
143
  lineChart();
140
144
 
@@ -196,8 +200,6 @@
196
200
 
197
201
  Log.d("kiteru?",deviceName);
198
202
 
199
- TextView nameText = findViewById(R.id.device_name_text);
200
-
201
203
  nameText.setText(deviceName);
202
204
 
203
205
  }