「Eclipse またはデバイス自体から最新のアプリを実行すると、Bluetooth を使用できません。問題のデバイスは、Bluetooth が有効になっている 2 台の Galaxy Nexus Phone (CDMA/LTE の両方) です。1 台は Android で AOKP を実行しています。 4.2.1、もう一方はストックで、Android 4.1.1 を搭載しています。
これらのデバイスには外部ストレージがなく、内部ストレージを/ mnt / sdcardにマウントするため、ハードコードされたMACアドレスが問題になる可能性があるBluetoothの他の問題に対する独自の解決策であると信じていました。ただし、ファイルから読み取る代わりに、MACアドレスをハードコーディングしようとしましたが、役に立ちませんでした。最初のリンクは MainActivity で、2 番目のリンクは Manifest.xml です (Bluetooth とストレージ (r/w) に対して適切なアクセス許可を持っています。3 番目のリンクに logcat を含めます。
主な活動:
package com.hyperspacecg.thermolitics;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final String PREFS_NAME = "ThermolyticsPrefsFile";
String Packet = null;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
SeekBar seekbarTemp, seekbarBlower;
TextView valueTemp, valueBlower;
RadioGroup ventilation;
private static int Blower = 0;
private static int Vent = 255;
private static int Temp = 0;
private static int Defrost = 0;
private static int AC = 0;
private static final UUID MY_UUID = UUID
.fromString("00001101-0000-1000-8000-00805F9B34FB");
// hardcoded MAC address
private static String address = null;
private BufferedReader buf;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
valueTemp = (TextView) findViewById(R.id.textViewTemp);
seekbarTemp = (SeekBar) findViewById(R.id.seekBarTemp);
valueBlower = (TextView) findViewById(R.id.textViewBlower);
seekbarBlower = (SeekBar) findViewById(R.id.seekBarBlower);
ventilation = (RadioGroup) findViewById(R.id.radioGroup1);
BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "Bluetooth is not available.",
Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this,
"Please enable your BT and re-run this program.",
Toast.LENGTH_LONG).show();
finish();
return;
}
int sbar_blower_position = settings.getInt("seekbar_blower_pref", 0);
seekbarBlower.setProgress(sbar_blower_position);
valueBlower.setText("Blower Power: " + sbar_blower_position);
Blower = sbar_blower_position;
int sbar_temp_position = settings.getInt("seekbar_temp_pref", 0);
seekbarTemp.setProgress(sbar_temp_position);
valueTemp.setText("Temperature: " + sbar_temp_position);
Temp = sbar_temp_position;
Vent = ventilation.indexOfChild(findViewById(ventilation.getCheckedRadioButtonId()));
seekbarBlower.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
valueBlower.setText("Blower Power: " + progress);
Blower = progress;
packet(Blower, Temp, Vent, AC, Defrost);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
seekbarTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
valueTemp.setText("Temperature:" + progress);
Temp = progress;
packet(Blower, Temp, Vent, AC, Defrost);
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
try{
File f = new File(Environment.getExternalStorageDirectory()+"/therm.txt");
FileInputStream fileIS = new FileInputStream(f);
buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = new String();
//just reading each line and pass it on the debugger
while((readString = buf.readLine())!= null){
address = readString;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
// When this returns, it will 'know' about the server,
// via it's MAC address.
address = "00:16:68:2B:40:90";
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
}
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
}
}
// Create a data stream so we can talk to server.
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try {
outStream.write(msgBuffer);
Toast.makeText(this, "BT successfully connected.",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
}
}
public void onClickDefrost(View v) throws IOException
{
if(Defrost == 1)
{
Defrost = 0;
}
else
{
Defrost = 1;
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try
{
outStream.write(msgBuffer);
}
catch (IOException e)
{
}
}
public void onClickAC(View v) throws IOException
{
if(AC == 1)
{
AC = 0;
}
else
{
AC = 1;
}
packet(Blower, Temp, Vent, AC, Defrost);
byte[] msgBuffer = Packet.getBytes();
try
{
outStream.write(msgBuffer);
}
catch (IOException e)
{
}
}
@Override
public void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("seekbar_blower_pref", Blower);
editor.putInt("seekbar_temp_pref", Temp);
editor.commit();
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
}
}
try {
btSocket.close();
} catch (IOException e2) {
}
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroy() {
super.onDestroy();
// Stop the Bluetooth chat services
}
public String packet(int Blower, int Temp, int Vent, int AC,int Defrost) {
Blower = Blower * 4;
Temp = Temp * 4;
Packet = "<HMG:" + Blower + ":" + Temp + ":" + Vent + ":" + AC + ":" + Defrost + ">";
return Packet;
}
}
マニフェスト:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hyperspacecg.thermolitics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.hyperspacecg.thermolitics.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ログキャット:
02-27 21:59:54.572: D/dalvikvm(8472): Late-enabling CheckJNI
02-27 21:59:54.689: E/Trace(8472): error opening trace file: No such file or directory (2)
02-27 21:59:55.041: D/dalvikvm(8472): GC_CONCURRENT freed 81K, 10% free 2696K/2964K, paused 2ms+6ms, total 115ms
02-27 21:59:55.236: D/dalvikvm(8472): GC_CONCURRENT freed 18K, 7% free 3078K/3308K, paused 12ms+12ms, total 69ms