1

我收到错误消息“从 Eclipse 或设备本身运行我的最新应用程序时,蓝牙不可用。有问题的设备是两部启用了蓝牙的 Galaxy Nexus 手机(CDMA/LTE)。一个正在使用 Android 运行 AOKP 4.2.1,另一个是现货,安卓4.1.1。

我曾相信我对其他蓝牙问题的独特解决方案需要硬编码的 MAC 地址可能是问题,因为这些设备没有外部存储,而是将内部存储安装到 /mnt/sdcard。但是,我尝试硬编码 mac 地址而不是从文件中读取它,但无济于事。第一个链接是我的 MainActivity,第二个链接是我的 Manifest.xml(我对蓝牙和存储 (r/w) 有适当的权限。我将在第三个链接中附上 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
4

0 回答 0