android端末上でのBluetoothのRSSI取得できるプログラムを作りたいと思い、参考にしようと思いあるサイト上からソースコードを引っ張ってきたのですが、エラーが起きてしまい解決できません。
エラーが起きている部分は、onCreate内の
setContentView(R.layout.main);のmainの部分にエラー
textView = (TextView) findViewById(R.id.textView1);のtextView1にエラー
の2か所です。
Java
1package com.example.bluetooth; 2 3import java.util.Set; 4 5import android.app.Activity; 6import android.bluetooth.BluetoothAdapter; 7import android.bluetooth.BluetoothDevice; 8import android.content.BroadcastReceiver; 9import android.content.Context; 10import android.content.Intent; 11import android.content.IntentFilter; 12import android.os.Bundle; 13import android.widget.TextView; 14 15public class MainActivity extends Activity { 16 private BluetoothAdapter myBluetoothAdapter; 17 private TextView textView; 18 19 /** Called when the activity is first created. */ 20 @Override 21 public void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.main); 24 25 textView = (TextView) findViewById(R.id.textView1); 26 27 // Bluetooth 28 myBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 29 if (myBluetoothAdapter == null) { 30 textView.append("Bluetoothをサポートしていません。\n"); 31 } else { 32 textView.append("Bluetoothをサポートしています。\n"); 33 //Bluetoothが利用できるか確認する 34 if (myBluetoothAdapter.isEnabled()) { 35 textView.append("Bluetoothを利用できます。\n"); 36 //自機ディバイスの調査 37 getLocalInformation(); 38 //ペアリング済みのディバイスの調査 39 findPairedDevices(); 40 //ディバイスを見つけて調査 41 discoverDevices(); 42 }else{ 43 textView.append("Bluetoothを利用できません。\n"); 44 //Bluetoothを使えるようにする処理を書いたりする。 45 } 46 } 47 } 48 49// 自機ディバイスの調査メソッド 50 51 private void getLocalInformation() { 52 // TODO 自動生成されたメソッド・スタブ 53 textView.append("自機Bluetoothディバイスの調査\n"); 54 textView.append("ディバイス名:" + myBluetoothAdapter.getName() + "\n"); 55 textView.append("アドレス:" + myBluetoothAdapter.getAddress() + "\n"); 56 57 textView.append("SCAN_MODE:"); 58 switch (myBluetoothAdapter.getScanMode()) { 59 case BluetoothAdapter.SCAN_MODE_CONNECTABLE: 60 textView.append("SCAN_MODE_CONNECTABLE" + "\n"); 61 break; 62 case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE: 63 textView.append("SCAN_MODE_CONNECTABLE_DISCOVERABLE" + "\n"); 64 break; 65 case BluetoothAdapter.SCAN_MODE_NONE: 66 textView.append("SCAN_MODE_NONE" + "\n"); 67 break; 68 } 69 70 textView.append("STATE:"); 71 switch (myBluetoothAdapter.getState()) { 72 case BluetoothAdapter.STATE_OFF: 73 textView.append("STATE_OFF\n"); 74 break; 75 case BluetoothAdapter.STATE_ON: 76 textView.append("STATE_ON\n"); 77 break; 78 case BluetoothAdapter.STATE_TURNING_OFF: 79 textView.append("STATE_TURNING_OFF\n"); 80 break; 81 case BluetoothAdapter.STATE_TURNING_ON: 82 textView.append("STATE_TURNING_ON\n"); 83 break; 84 } 85 } 86 87// ペアリング済みの調査 88 private void findPairedDevices() { 89 textView.append("\n登録済みディバイス\n"); 90 91 Set<BluetoothDevice> pairedDevices = myBluetoothAdapter.getBondedDevices(); 92 if (pairedDevices.size() > 0) { 93 for (BluetoothDevice device : pairedDevices) { 94 textView.append("ディバイス名:" + device.getName() + "\n"); 95 textView.append("アドレス:" + device.getAddress() + "\n"); 96 textView.append("クラス:" + device.getBluetoothClass() + "\n"); 97 } 98 } else { 99 textView.append("登録されているBluetoothデバイスはありません。\n"); 100 } 101 } 102 103// 他のBluetoothディバイスを見つけるレシーバーを登録 104 105 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 106 public void onReceive(Context context, Intent intent) { 107 String action = intent.getAction(); 108 // When discovery finds a device 109 if (BluetoothDevice.ACTION_FOUND.equals(action)) { 110 // Get the BluetoothDevice object from the Intent 111 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 112 textView.append("ディバイス名: " + device.getName() + "\n"); 113 textView.append("アドレス: " + device.getAddress() + "\n"); 114 short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE); 115 textView.append("RSSI: " + rssi + "dBm" + "\n"); 116 } 117 } 118 }; 119 120 121// ディバイスを見つける 122 private void discoverDevices() { 123 // TODO 自動生成されたメソッド・スタブ 124 125 IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); 126 registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy 127 128 myBluetoothAdapter.startDiscovery(); 129 textView.append("\nディバイスを検索中\n"); 130 } 131}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/12 14:39
2016/12/12 14:45
2016/12/12 14:58
2016/12/12 15:12
2016/12/12 15:23
2016/12/12 15:27
2016/12/12 15:46
2016/12/12 23:54