回答編集履歴

2

SS追加

2025/01/16 12:28

投稿

jimbe
jimbe

スコア13286

test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  発信:リュウド株式会社 ReUdo R1Beacon (RE-BC-BLE401W-1) BluetoothLE4.0
6
6
  受信:SONY Xperia XZ3 (au) Android10, Bluetooth5.0
7
+
8
+ ![受信時スクリーンショット](https://ddjkaamml8q8x.cloudfront.net/questions/2025-01-16/e0a7b163-84c9-46bc-ab9f-e05e77ac1141.png)
7
9
 
8
10
  AndroidManifest.xml へのパーミッション等の追加
9
11
  ```xml

1

追加修正

2025/01/16 09:10

投稿

jimbe
jimbe

スコア13286

test CHANGED
@@ -1,19 +1,32 @@
1
- **※回答でありません。**
2
-
3
- 質問へのコメントを元に、 m._.i さんが参考にされている記事の最後のプログラムを手元の環境(+altbeacon 2.20.6)で非推奨等を使わないように作り直してみました。が、手元には iBeacon の送信機が無いので受信テスト出来ません。
1
+ 質問へのコメントを元に、 m._.i さんが参考にされている記事の最後のプログラムを手元の環境(+altbeacon 2.20.6)で非推奨等を使わないように作り直してみました。~~が、手元には iBeacon の送信機が無いので受信テスト出来ません。~~
4
- お時間ありましたら実行してみて頂けないでしょうか。
2
+ ~~お時間ありましたら実行してみて頂けないでしょうか。~~
5
- こちらでは現在の所実行すると数秒おきに "InRegion 0" が出続けるようになります。
3
+ ~~こちらでは現在の所実行すると数秒おきに "InRegion 0" が出続けるようになります。~~
4
+
6
-
5
+ 発信:リュウド株式会社 ReUdo R1Beacon (RE-BC-BLE401W-1) BluetoothLE4.0
6
+ 受信:SONY Xperia XZ3 (au) Android10, Bluetooth5.0
7
+
8
+ AndroidManifest.xml へのパーミッション等の追加
9
+ ```xml
10
+ <uses-permission android:name="android.permission.BLUETOOTH" />
11
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
12
+
13
+ <uses-feature android:name="android.hardware.bluetooth_le" android:required="true" />
14
+ ```
7
15
  MainActivity.java
8
16
  ```java
17
+ import static androidx.activity.result.contract.ActivityResultContracts.*;
18
+
19
+ import android.Manifest;
20
+ import android.content.pm.PackageManager;
9
21
  import android.database.DataSetObserver;
10
22
  import android.os.*;
11
23
  import android.util.Log;
12
24
  import android.view.*;
13
25
  import android.widget.*;
14
26
 
27
+ import androidx.activity.result.ActivityResultLauncher;
15
28
  import androidx.annotation.NonNull;
16
- import androidx.appcompat.app.AppCompatActivity;
29
+ import androidx.appcompat.app.*;
17
30
  import androidx.lifecycle.*;
18
31
 
19
32
  import org.altbeacon.beacon.*;
@@ -22,12 +35,29 @@
22
35
 
23
36
  public class MainActivity extends AppCompatActivity {
24
37
  private static final String LOG_TAG = "MainActivity";
38
+
39
+ private final ActivityResultLauncher<String[]> launcher =
40
+ registerForActivityResult(new RequestMultiplePermissions(), granted -> {
41
+ if(granted.get(Manifest.permission.ACCESS_FINE_LOCATION) == Boolean.TRUE) {
42
+ start();
43
+ } else {
44
+ finish();
45
+ }
46
+ });
25
47
 
26
48
  @Override
27
49
  protected void onCreate(Bundle savedInstanceState) {
28
50
  super.onCreate(savedInstanceState);
29
51
  setContentView(R.layout.activity_main);
30
52
 
53
+ if(checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
54
+ start();
55
+ } else {
56
+ launcher.launch(new String[]{Manifest.permission.ACCESS_FINE_LOCATION});
57
+ }
58
+ }
59
+
60
+ void start() {
31
61
  TextView resultText = findViewById(R.id.result);
32
62
  ListView listView = findViewById(R.id.beacon_list);
33
63
 
@@ -45,10 +75,10 @@
45
75
  }
46
76
  });
47
77
 
48
- settingBeacon(adapter);
78
+ settingAltBeacon(adapter);
49
- }
79
+ }
50
-
80
+
51
- private void settingBeacon(BeaconAdapter adapter) {
81
+ private void settingAltBeacon(BeaconAdapter adapter) {
52
82
  BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
53
83
  String BEACON_LAYOUT = "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24";
54
84
  beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BEACON_LAYOUT));
@@ -84,29 +114,29 @@
84
114
 
85
115
  beaconManager.addRangeNotifier((beacons, region) -> {
86
116
  Log.d(LOG_TAG, "InRegion " + beacons.size());
117
+ List<BeaconItem> list = new ArrayList<>();
118
+ for(Beacon beacon : beacons) list.add(new BeaconItem(beacon));
119
+ list.sort(Comparator.comparingDouble(a -> a.distance));
87
- adapter.set(beacons);
120
+ adapter.set(list);
88
121
  });
89
122
  }
90
123
 
91
124
  private static class BeaconAdapter extends BaseAdapter {
92
- private final List<BeaconItem> beaconItemList = new ArrayList<>();
125
+ private List<BeaconItem> itemList = new ArrayList<>();
93
-
126
+
94
- void set(Collection<Beacon> beacons) {
127
+ void set(@NonNull List<BeaconItem> list) {
95
- beaconItemList.clear();
128
+ itemList = new ArrayList<>(list);
96
- if(beacons != null) {
97
- for(Beacon beacon : beacons) beaconItemList.add(new BeaconItem(beacon));
98
- }
99
129
  notifyDataSetChanged();
100
130
  }
101
131
 
102
132
  @Override
103
133
  public int getCount() {
104
- return beaconItemList.size();
134
+ return itemList.size();
105
135
  }
106
136
 
107
137
  @Override
108
138
  public Object getItem(int position) {
109
- return beaconItemList.get(position);
139
+ return itemList.get(position);
110
140
  }
111
141
 
112
142
  @Override
@@ -117,12 +147,12 @@
117
147
  @Override
118
148
  public View getView(int position, View convertView, ViewGroup parent) {
119
149
  ViewHolder vh = convertView == null ? new ViewHolder(parent) : (ViewHolder)convertView.getTag();
120
- return vh.bind(beaconItemList.get(position));
150
+ return vh.bind(itemList.get(position));
121
151
  }
122
152
 
123
153
  private static class ViewHolder {
124
- final View itemView;
154
+ private final View itemView;
125
- final TextView beacon;
155
+ private final TextView beacon;
126
156
 
127
157
  ViewHolder(ViewGroup parent) {
128
158
  itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.beacon_view, parent, false);
@@ -133,29 +163,29 @@
133
163
 
134
164
  View bind(BeaconItem item) {
135
165
  beacon.setText(String.format(Locale.getDefault(),
136
- "UUID%s\nmajor%s minor%s\nRSSI%d TxPower%d Distance%f",
166
+ "UUID:%s\nmajor:%s minor:%s\nRSSI:%d TxPower:%d Distance:%f",
137
167
  item.uuid.toString(), item.major.toString(), item.minor.toString(),
138
168
  item.rssi, item.txPower, item.distance));
139
169
  return itemView;
140
170
  }
141
171
  }
142
-
172
+ }
173
+
143
- private static class BeaconItem {
174
+ private static class BeaconItem {
144
- final Identifier uuid, major, minor;
175
+ final Identifier uuid, major, minor;
145
- final int rssi, txPower;
176
+ final int rssi, txPower;
146
- final double distance;
177
+ final double distance;
147
-
178
+
148
- BeaconItem(Beacon beacon) {
179
+ BeaconItem(Beacon beacon) {
149
- this(beacon.getId1(), beacon.getId2(), beacon.getId3(), beacon.getRssi(), beacon.getTxPower(), beacon.getDistance());
180
+ this(beacon.getId1(), beacon.getId2(), beacon.getId3(), beacon.getRssi(), beacon.getTxPower(), beacon.getDistance());
150
- }
181
+ }
151
- BeaconItem(Identifier uuid, Identifier major, Identifier minor, int rssi, int txPower, double distance) {
182
+ BeaconItem(Identifier uuid, Identifier major, Identifier minor, int rssi, int txPower, double distance) {
152
- this.uuid = uuid;
183
+ this.uuid = uuid;
153
- this.major = major;
184
+ this.major = major;
154
- this.minor = minor;
185
+ this.minor = minor;
155
- this.rssi = rssi;
186
+ this.rssi = rssi;
156
- this.txPower = txPower;
187
+ this.txPower = txPower;
157
- this.distance = distance;
188
+ this.distance = distance;
158
- }
159
189
  }
160
190
  }
161
191
  }