2014-01-09 66 views
0
package br.com.buceta; 

import java.io.IOException; 
import java.io.OutputStream; 
import java.lang.reflect.Method; 
import java.util.UUID; 

import android.app.Activity; 
import android.bluetooth.BluetoothAdapter; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.content.Intent; 
import android.os.Build; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private static final String TAG = "bluetooth1"; 

    Button btnOn, btnOff; 

    private BluetoothAdapter btAdapter = null; 
    private BluetoothSocket btSocket = null; 
    private OutputStream outStream = null; 

    // SPP UUID service 
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    // MAC-address of Bluetooth module (you must edit this line) 
    private static String address = "00:15:FF:F2:19:5F"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); (error) 

    btnOn = (Button) findViewById(R.id.btnOn); (error) 
    btnOff = (Button) findViewById(R.id.btnOff); (error) 

    btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    checkBTState(); 

    btnOn.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
     sendData("1"); 
     Toast.makeText(getBaseContext(), "Turn on LED", Toast.LENGTH_SHORT).show(); 
    } 
}); 

btnOff.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { 
    sendData("0"); 
    Toast.makeText(getBaseContext(), "Turn off LED", Toast.LENGTH_SHORT).show(); 
    } 
}); 
    } 

    private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException { 
     if(Build.VERSION.SDK_INT >= 10){ 
      try { 
      final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class }); 
      return (BluetoothSocket) m.invoke(device, MY_UUID); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not create Insecure RFComm Connection",e); 
     } 
    } 
    return device.createRfcommSocketToServiceRecord(MY_UUID); 
    } 

    @Override 
    public void onResume() { 
super.onResume(); 

Log.d(TAG, "...onResume - try connect..."); 

// Set up a pointer to the remote node using it's address. 
BluetoothDevice device = btAdapter.getRemoteDevice(address); 

// Two things are needed to make a connection: 
// A MAC address, which we got above. 
// A Service ID or UUID. In this case we are using the 
//  UUID for SPP. 

try { 
    btSocket = createBluetoothSocket(device); 
} catch (IOException e1) { 
    errorExit("Fatal Error", "In onResume() and socket create failed: " + e1.getMessage() + "."); 
    } 

// Discovery is resource intensive. Make sure it isn't going on 
// when you attempt to connect and pass your message. 
btAdapter.cancelDiscovery(); 

// Establish the connection. This will block until it connects. 
Log.d(TAG, "...Connecting..."); 
try { 
    btSocket.connect(); 
    Log.d(TAG, "...Connection ok..."); 
} catch (IOException e) { 
    try { 
    btSocket.close(); 
    } catch (IOException e2) { 
    errorExit("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + "."); 
    } 
} 

// Create a data stream so we can talk to server. 
Log.d(TAG, "...Create Socket..."); 

try { 
    outStream = btSocket.getOutputStream(); 
} catch (IOException e) { 
    errorExit("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + "."); 
} 
    } 

    @Override 
    public void onPause() { 
super.onPause(); 

Log.d(TAG, "...In onPause()..."); 

if (outStream != null) { 
    try { 
    outStream.flush(); 
    } catch (IOException e) { 
    errorExit("Fatal Error", "In onPause() and failed to flush output stream: " + e.getMessage() + "."); 
    } 
} 

try  { 
    btSocket.close(); 
} catch (IOException e2) { 
    errorExit("Fatal Error", "In onPause() and failed to close socket." + e2.getMessage() + "."); 
} 
     } 

    private void checkBTState() { 
// Check for Bluetooth support and then check to make sure it is turned on 
// Emulator doesn't support Bluetooth and will return null 
    if(btAdapter==null) { 
    errorExit("Fatal Error", "Bluetooth not support"); 
} else { 
    if (btAdapter.isEnabled()) { 
    Log.d(TAG, "...Bluetooth ON..."); 
    } else { 
    //Prompt user to turn on Bluetooth 
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    startActivityForResult(enableBtIntent, 1); 
    } 
} 
    } 

    private void errorExit(String title, String message){ 
Toast.makeText(getBaseContext(), title + " - " + message, Toast.LENGTH_LONG).show(); 
finish(); 
    } 

    private void sendData(String message) { 
byte[] msgBuffer = message.getBytes(); 

Log.d(TAG, "...Send data: " + message + "..."); 

try { 
    outStream.write(msgBuffer); 
} catch (IOException e) { 
    String msg = "In onResume() and an exception occurred during write: " + e.getMessage(); 
    if (address.equals("00:00:00:00:00:00")) 
    msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 35 in the java code"; 
    msg = msg + ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n"; 

    errorExit("Fatal Error", msg);  
} 
    } 
} 

有一個在R.layout.activity_main沒有錯誤,但代碼無法識別activity_main.xml中我已經宣佈的按鈕。R 1不能被解析爲一個變量 -

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<Button 
    android:id="@+id/btnOff" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="@string/btn_OFF" /> 

<Button 
    android:id="@+id/btnOn" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/btnOff" 
    android:layout_centerHorizontal="true" 
    android:text="@string/btn_ON" /> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:alpha="0.5" 
    android:src="@drawable/cxemnet_logo" /> 

</RelativeLayout>  

詩:我通過清洗解決了「R不能解析爲一個類型」錯誤>建築項目,然後這個錯誤出現。

+1

你的進口報表是什麼樣的? – Yjay

+0

什麼是錯誤信息? – Prem

+0

btnOn無法解析或不是字段而btnOff無法解析或不是字段 – cleon

回答

1

你必須聲明你的按鈕。要麼你在你的方法

... 
    setContentView(R.layout.activity_main); 
    Button btnOn = findViewById(R.id.btnOn); 
    Button btnOff = findViewById(R.id.btnOff); 
    ... 

或本地外地

public class Classname { 

     private Button btnOn; 
     private Button btnOff; 

     ... 
    } 
0

從項目中刪除gen文件夾,然後清理整個項目也刪除用於活動類中使用r.class的導入語句。

0

檢查您的清單文件。你可能忘了宣佈一些東西。通常是一個活動。

0

Android將無法正確生成R檔,如果沒有在XML文件中的任何錯誤申報。也許錯誤在其中之一(即佈局文件):你關閉了所有的標籤?有沒有不受支持的屬性? 也試圖組織進口(在Windows的快捷鍵是CTRL + MAIUSC + Ø [這是一個聲音,而不是0],在Mac是CMD + MAIUSC + Ø

0

Eclipse自動添加了「R」導入。除非您嘗試引用另一個軟件包中的資源,否則不需要並可以將其刪除。

例如,如果你有一個包com.package2但你的表現有com.package1包名稱,然後在com.package2任何類將需要:

import com.package1.R; 

你的情況,這似乎是自動添加。刪除它(不要使用「組織導入」)和「乾淨」

+0

如果導入不是必需的,並且使用「組織導入」,那麼eclipse會自動刪除導入。所以,在我看來,這是一個很好的選擇。 – user2340612

+0

當導入「R」文件時,Eclipse上的某些版本/構建文件會引發錯誤,因此「組織導入」可能沒有問題。無論如何,獲得一個乾淨的構建是重要的,並且導入是問題。 – Jim

相關問題